Skip to content

Commit d284517

Browse files
Merge pull request #4539 from SciML/as/faster-iprobpmap
fix: avoid generating massive functions in `get_mtkparameters_reconstructor`
2 parents 052a8d1 + 1f026bd commit d284517

5 files changed

Lines changed: 273 additions & 71 deletions

File tree

lib/ModelingToolkitBase/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800"
7373
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
7474
Mooncake = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6"
7575
Pyomo = "0e8e1daf-01b5-4eba-a626-3897743a3816"
76+
Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"
7677

7778
[extensions]
7879
MTKBifurcationKitExt = "BifurcationKit"
@@ -86,6 +87,7 @@ MTKLabelledArraysExt = "LabelledArrays"
8687
MTKLatexifyExt = "Latexify"
8788
MTKMooncakeExt = "Mooncake"
8889
MTKPyomoDynamicOptExt = "Pyomo"
90+
MTKTrackerExt = "Tracker"
8991

9092
[compat]
9193
ADTypes = "1.14.0"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module MTKTrackerExt
2+
3+
import ModelingToolkitBase as MTKBase
4+
import Tracker
5+
6+
function MTKBase.promote_type_with_nothing(::Type{Tracker.TrackedReal{T}}, x::Tracker.TrackedArray{T}) where {T}
7+
return Tracker.TrackedReal{T}
8+
end
9+
10+
function MTKBase.promote_with_nothing(::Type{Tracker.TrackedReal{T}}, x::Tracker.TrackedArray{T}) where {T}
11+
return x
12+
end
13+
14+
end

lib/ModelingToolkitBase/src/systems/abstractsystem.jl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -626,12 +626,17 @@ end
626626
"""
627627
Returns true if the parameter `p` is of the form `Initial(x)`.
628628
"""
629+
function isinitial(p::SymbolicT)
630+
p, _ = split_indexed_var(p)
631+
Moshi.Match.@match p begin
632+
BSImpl.Term(; f) => f isa Initial
633+
_ => false
634+
end
635+
end
629636
function isinitial(p)
630-
p = unwrap(p)
631-
return iscall(p) && (
632-
operation(p) isa Initial ||
633-
operation(p) === getindex && isinitial(arguments(p)[1])
634-
)
637+
up = unwrap(p)
638+
up === p && return false
639+
return isinitial(up)
635640
end
636641

637642
"""

lib/ModelingToolkitBase/src/systems/nonlinear/initializesystem.jl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -812,14 +812,9 @@ function DiffEqBase.get_updated_symbolic_problem(
812812
end
813813

814814
u0 = DiffEqBase.promote_u0(u0, buffer, t0)
815+
u0 = ArrayInterface.restructure(u0, meta.get_updated_u0(prob, initdata.initializeprob))
815816

816-
if ArrayInterface.ismutable(u0)
817-
T = typeof(u0)
818-
else
819-
T = StaticArrays.similar_type(u0)
820-
end
821-
822-
return remake(prob; u0 = T(meta.get_updated_u0(prob, initdata.initializeprob)), p)
817+
return remake(prob; u0, p)
823818
end
824819

825820
"""

0 commit comments

Comments
 (0)