From 9ac9ba423091d42ddf0aa798ca1bd1aa799121dd Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Wed, 17 Jul 2024 11:23:03 +0200 Subject: [PATCH 1/5] Add utility function that unites two things that may be nothing --- src/systems/abstractsystem.jl | 5 +---- src/utils.jl | 11 +++++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 6cdcd72855..c9518bc2af 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2521,10 +2521,7 @@ function extend(sys::AbstractSystem, basesys::AbstractSystem; name::Symbol = nam eqs = union(get_eqs(basesys), get_eqs(sys)) sts = union(get_unknowns(basesys), get_unknowns(sys)) ps = union(get_ps(basesys), get_ps(sys)) - base_deps = parameter_dependencies(basesys) - deps = parameter_dependencies(sys) - dep_ps = isnothing(base_deps) ? deps : - isnothing(deps) ? base_deps : union(base_deps, deps) + dep_ps = union_nothing(parameter_dependencies(basesys), 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)) diff --git a/src/utils.jl b/src/utils.jl index dc239e34c1..dc6f0a0e6f 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1,3 +1,14 @@ +""" + union_nothing(x::Union{T1, Nothing}, y::Union{T2, Nothing}) where {T1, T2} + +Unite x and y gracefully when they could be nothing. If neither is nothing, x and y are united normally. If one is nothing, the other is returned unmodified. If both are nothing, nothing is returned. +""" +function union_nothing(x::Union{T1, Nothing}, y::Union{T2, Nothing}) where {T1, T2} + isnothing(x) && return y # y can be nothing or something + isnothing(y) && return x # x can be nothing or something + return union(x, y) # both x and y are something and can be united normally +end + get_iv(D::Differential) = D.x function make_operation(@nospecialize(op), args) From bcc781b42bca4ba83c018357eb7c60f165ab9f3b Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Wed, 17 Jul 2024 11:24:00 +0200 Subject: [PATCH 2/5] Extend metadata, too --- 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 c9518bc2af..2798349ae6 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2526,10 +2526,11 @@ 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` + 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, + discrete_events = devs, defaults = defs, systems = syss, metadata = meta, name = name, gui_metadata = gui_metadata) # collect fields specific to some system types From bac931ddc7ec6c988b55f4036a2c21331d532451 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Wed, 17 Jul 2024 11:24:12 +0200 Subject: [PATCH 3/5] Test metadata extension --- test/odesystem.jl | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/odesystem.jl b/test/odesystem.jl index ab28f5cb47..e8c3414cf5 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1194,3 +1194,17 @@ end @test_nowarn obsfn(buffer, [1.0], ps..., 3.0) @test buffer ≈ [2.0, 3.0, 4.0] 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) + @named A1 = ODESystem(Equation[], t, [], []) + @named B1 = ODESystem(Equation[], t, [], []) + @named A2 = ODESystem(Equation[], t, [], []; metadata = A) + @named B2 = ODESystem(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) +end \ No newline at end of file From 3c101cfb3bbac25b700b17d362d75bc627a42ca8 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Wed, 17 Jul 2024 11:25:46 +0200 Subject: [PATCH 4/5] Format --- test/odesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/odesystem.jl b/test/odesystem.jl index e8c3414cf5..d7b22d30b2 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1207,4 +1207,4 @@ end @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) -end \ No newline at end of file +end From e6be2a7dc30829912f103af150e49bfefbf5690c Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Wed, 17 Jul 2024 12:02:50 +0200 Subject: [PATCH 5/5] Format --- test/odesystem.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/odesystem.jl b/test/odesystem.jl index 37a7d08dab..d54baa8a93 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -1203,9 +1203,9 @@ end @named B1 = ODESystem(Equation[], t, [], []) @named A2 = ODESystem(Equation[], t, [], []; metadata = A) @named B2 = ODESystem(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 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) end