Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parameter dependencies across model hierarchy #2978

Merged
merged 4 commits into from
Aug 27, 2024

Conversation

SebastianM-C
Copy link
Contributor

@SebastianM-C SebastianM-C commented Aug 20, 2024

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

Parameter dependencies can get broken if parameter_dependencies(sys::AbstractSystem) returns Vector{Any}, as process_parameter_dependencies(pdeps, ps) checks the element type

if !(eltype(pdeps) <: Equation)
error("Parameter dependencies must be a `Dict`, `Vector{Pair}` or `Vector{Equation}`")
end

This PR adds a type assert in equation namespaceing and makes parameter_dependencies infer better.

julia> @code_warntype ModelingToolkit.parameter_dependencies(sys1)
MethodInstance for ModelingToolkit.parameter_dependencies(::ODESystem)
  from parameter_dependencies(sys::ModelingToolkit.AbstractSystem) @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/abstractsystem.jl:1342
Arguments
  #self#::Core.Const(ModelingToolkit.parameter_dependencies)
  sys::ODESystem
Locals
  #1409::ModelingToolkit.var"#1409#1411"
  namespaced_deps::Vector{Equation}
  systems::Vector{ODESystem}
  pdeps::Vector{Equation}
Body::Vector{Equation}
1 ─       Core.NewvarNode(:(#1409))
│         Core.NewvarNode(:(namespaced_deps))
│         Core.NewvarNode(:(systems))
│         Core.NewvarNode(:(pdeps))
│   %5  = ModelingToolkit.:!::Core.Const(!)
│   %6  = ModelingToolkit.has_parameter_dependencies(sys)::Core.Const(true)
│   %7  = (%5)(%6)::Core.Const(false)
└──       goto #3 if not %7
2 ─       Core.Const(:(Base.getindex(ModelingToolkit.Equation)))
└──       Core.Const(:(return %9))
3 ┄       (pdeps = ModelingToolkit.get_parameter_dependencies(sys))
│         (systems = ModelingToolkit.get_systems(sys))
│   %13 = ModelingToolkit.:(var"#1409#1411")::Core.Const(ModelingToolkit.var"#1409#1411")
│         (#1409 = %new(%13))
│   %15 = #1409::Core.Const(ModelingToolkit.var"#1409#1411"())
│   %16 = (:init,)::Core.Const((:init,))
│   %17 = Core.apply_type(Core.NamedTuple, %16)::Core.Const(NamedTuple{(:init,)})
│   %18 = Base.getindex(ModelingToolkit.Equation)::Vector{Equation}
│   %19 = Core.tuple(%18)::Tuple{Vector{Equation}}
│   %20 = (%17)(%19)::@NamedTuple{init::Vector{Equation}}
│   %21 = systems::Vector{ODESystem}
│         (namespaced_deps = Core.kwcall(%20, ModelingToolkit.mapreduce, %15, ModelingToolkit.vcat, %21))
│   %23 = namespaced_deps::Vector{Equation}
│   %24 = pdeps::Vector{Equation}
│   %25 = ModelingToolkit.vcat(%23, %24)::Vector{Equation}
└──       return %25

One case that shows this issue is having parameter dependencies that reference a subsystem (and this didn't have a test anyway, so I added that).

Note that while #2966 also made changes in this area, the test case that I am adding was not fixed by that. I had an initial version of this PR which would split off the empty case, but after the recursion, but splitting that off should no longer be needed as type inference on mapping namespace_equation is better and we should always have Vector{Equation}.

@SebastianM-C SebastianM-C force-pushed the smc/quick_fix branch 2 times, most recently from 4a62468 to 9ca10d8 Compare August 21, 2024 11:55
@AayushSabharwal
Copy link
Member

AayushSabharwal commented Aug 21, 2024

This is failing CI though

EDIT: Wait, master errors this way too.

@SebastianM-C
Copy link
Contributor Author

@ChrisRackauckas I rebased the PR

Copy link
Member

@AayushSabharwal AayushSabharwal left a comment

Choose a reason for hiding this comment

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

Format?

@SebastianM-C
Copy link
Contributor Author

I saw that you're doing it in 2a298d0, so I didn't do it to avoid conflicts 😅

@ChrisRackauckas ChrisRackauckas merged commit 415f62e into SciML:master Aug 27, 2024
20 of 23 checks passed
@SebastianM-C SebastianM-C deleted the smc/quick_fix branch August 27, 2024 13:18
@AayushSabharwal
Copy link
Member

Conflicts are a problem for whoever merges last :P Also, in this specific case that commit would just end up being dropped

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants