Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
145 commits
Select commit Hold shift + click to select a range
8eb2d68
fix: fix `ODEProblem` construction during precompilation with `eval_e…
AayushSabharwal Oct 6, 2025
fba3ccc
fix: fix overspecialization of nonnumeric buffers in `remake_buffer`
AayushSabharwal Oct 8, 2025
638a7a9
fix: fix `eval_expression = true` construction of problems
AayushSabharwal Oct 8, 2025
83db1e5
test: test no specialization of nonnumeric buffers
AayushSabharwal Oct 9, 2025
135383b
test: test solving `ODEProblem` with `eval_expression = true`
AayushSabharwal Oct 9, 2025
af71cca
docs: fix bad docstring
AayushSabharwal Oct 9, 2025
aaaac6e
fix: fix type-change of time-dependent parameters with `respecialize`
AayushSabharwal Oct 10, 2025
9ede8ab
fix: add `@invokelatest` to allow trivial initialization with `eval_e…
AayushSabharwal Oct 10, 2025
eb1c901
test: fix `respecialize` test
AayushSabharwal Oct 10, 2025
9afe05e
refactor: remove usages of `Symbolic`
AayushSabharwal Sep 30, 2025
f04e9e7
refactor: remove usages of `_parse_vars`
AayushSabharwal Sep 30, 2025
4d42365
refactor: remove outdated imports
AayushSabharwal Sep 30, 2025
194831c
refactor: remove usages of `fast_substitute`
AayushSabharwal Sep 30, 2025
2cb147b
refactor: remove usages of `CallWithMetadata`
AayushSabharwal Sep 30, 2025
36ea1d8
refactor: remove usages of old `symtype` syntax
AayushSabharwal Sep 30, 2025
51440fd
refactor: remove usages of `Difference`
AayushSabharwal Sep 30, 2025
e0b6e72
refactor: remove usages of `Symbolics._mapreduce`
AayushSabharwal Sep 30, 2025
eafd2da
refactor: remove usages of `Symbolics.getparent`
AayushSabharwal Sep 30, 2025
251ee2b
fix: implement `SII.getname` for `System`
AayushSabharwal Sep 30, 2025
334dcfd
fix: remove usages of `Symbolics.Unknown`, `Symbolics.shape`
AayushSabharwal Sep 30, 2025
d820617
refactor: remove usages of `array_term`
AayushSabharwal Sep 30, 2025
c0e5e02
fix: move ChainRulesCore to an extension
AayushSabharwal Oct 1, 2025
99ebed1
fix: move JuliaFormatter to an extension
AayushSabharwal Oct 1, 2025
60e5f5f
fix: fix invalidations from `promote_symtype` method
AayushSabharwal Oct 1, 2025
f7533e2
fix: remove usages of `occursin` for searching expressions
AayushSabharwal Oct 1, 2025
04c7e95
fix: remove usages of deprecated `children`
AayushSabharwal Oct 1, 2025
0723840
refactor: concretely type some utility functions
AayushSabharwal Oct 1, 2025
b5b373b
fix: handle `Const` in connection expansion
AayushSabharwal Oct 1, 2025
b3f1d32
refactor: improve type-stability of `renamespace`
AayushSabharwal Oct 1, 2025
89cf3a7
refactor: make `System` more concretely typed
AayushSabharwal Oct 1, 2025
5320702
fix: fix usage of new `get_variables` in `is_bound`
AayushSabharwal Oct 1, 2025
0a8a58b
fix: handle removal of `operator` kwarg of `substitute`
AayushSabharwal Oct 1, 2025
bb5138b
refactor: get `System` to precompile in a trivial case
AayushSabharwal Oct 1, 2025
866b2a3
fix: improve inference of variable metadata accessors
AayushSabharwal Oct 2, 2025
65dc9ae
fix: make `default_toterm` type stable
AayushSabharwal Oct 2, 2025
9a62027
fix: improve inference of several utility functions
AayushSabharwal Oct 2, 2025
85a1916
fix: make `shift2term` type-stable
AayushSabharwal Oct 2, 2025
99d1c00
fix: make `unhack_observed` type-stable
AayushSabharwal Oct 2, 2025
0d11876
fix: make `independent_variables` type-stable
AayushSabharwal Oct 2, 2025
c72bc8a
fix: improve type-stability of some SII functions
AayushSabharwal Oct 2, 2025
30c6a0b
fix: improve type-stability of the `Initial` operator
AayushSabharwal Oct 2, 2025
eecb9c5
fix: make `add_initialization_parameters` type-stable
AayushSabharwal Oct 2, 2025
cb16cef
fix: improve type-stability of `discover_globalscoped`
AayushSabharwal Oct 2, 2025
a8ba1cb
fix: make topsorting equations type-stable
AayushSabharwal Oct 2, 2025
d0aac55
fix: make `SymbolicAffect` and `AffectSystem` type-stable
AayushSabharwal Oct 2, 2025
38d28e5
fix: improve type-stability of `ImperativeAffect`
AayushSabharwal Oct 2, 2025
ef6f531
fix: make `reorder_parameters` more type-stable
AayushSabharwal Oct 2, 2025
b4d0b35
fix: make some SII impls of `IndexCache` more type-stable
AayushSabharwal Oct 2, 2025
afd19da
fix: make `IndexCache` constructor more type-stable
AayushSabharwal Oct 2, 2025
d279aaa
fix: improve precompile-friendliness of `complete`
AayushSabharwal Oct 2, 2025
ad058d3
refactor: miscellaneous improvements
AayushSabharwal Oct 2, 2025
4af9d7c
fix: minor fix for `evaluate_varmap!`
AayushSabharwal Oct 2, 2025
ee520b3
fix: minor type-stability improvements to `hasbounds`
AayushSabharwal Oct 6, 2025
b6058b2
fix: minor type-stability improvement to `isoperator`
AayushSabharwal Oct 6, 2025
97d6f89
fix: make `flatten_equations` type-stable
AayushSabharwal Oct 6, 2025
550c106
fix: make `isparameter` type stable
AayushSabharwal Oct 6, 2025
01f298e
fix: make `input_timedomain` type-stable
AayushSabharwal Oct 6, 2025
c2f3b5f
fix: improve type-stability of `default_consolidate`, `flatten`
AayushSabharwal Oct 6, 2025
d74eeef
fix: improve type-stability of connection infrastructure
AayushSabharwal Oct 6, 2025
5fba498
fix: improve type-stability of `getvar`
AayushSabharwal Oct 6, 2025
f702020
fix: improve type-stability of aggregator functions
AayushSabharwal Oct 6, 2025
5aeee69
fix: improve type-stability of `noise_to_brownians`
AayushSabharwal Oct 6, 2025
7f8f9dc
fix: improve type-stability of `simplify_optimization_system`
AayushSabharwal Oct 6, 2025
a49be16
fix: improve type-stability of `TearingState`
AayushSabharwal Oct 6, 2025
4073290
fix: improve type-stability of `pantelides_reassemble`
AayushSabharwal Oct 7, 2025
055eedf
fix: improve type-stability of `tearing_reassemble`
AayushSabharwal Oct 7, 2025
25a4116
fix: improve type-stability of structural transformation utils
AayushSabharwal Oct 7, 2025
e5132c8
fix: improve type-stability of alias elimination
AayushSabharwal Oct 7, 2025
27c893a
fix: improve type-stability of clock inference
AayushSabharwal Oct 7, 2025
e8d3e87
fix: improve type-stability of `Schedule` struct
AayushSabharwal Oct 7, 2025
ed4a431
fix: improve type-stability of `\itdneighbors`
AayushSabharwal Oct 7, 2025
9c7522b
fix: improve type-stability of IO utils
AayushSabharwal Oct 7, 2025
119565b
fix: improve type-stability of `mtkcompile`
AayushSabharwal Oct 7, 2025
891fde3
refactor: update old `get_variables!` methods to `search_variables!`
AayushSabharwal Oct 10, 2025
357fbf8
refactor: remove dead code in `unhack_observed`
AayushSabharwal Oct 10, 2025
beac316
fix: improve type-stability of 2-arg `System` constructor
AayushSabharwal Oct 10, 2025
d07b093
fixup! fix: improve type-stability of some SII functions
AayushSabharwal Oct 10, 2025
fb899b2
fixup! fix: make `add_initialization_parameters` type-stable
AayushSabharwal Oct 10, 2025
46ea268
fixup! fix: improve precompile-friendliness of `complete`
AayushSabharwal Oct 10, 2025
d05341a
fixup! refactor: improve type-stability of `renamespace`
AayushSabharwal Oct 10, 2025
739e1b5
fixup! fix: improve precompile-friendliness of `complete`
AayushSabharwal Oct 10, 2025
8889a67
fixup! fix: improve precompile-friendliness of `complete`
AayushSabharwal Oct 10, 2025
d95e445
fixup! fix: improve precompile-friendliness of `complete`
AayushSabharwal Oct 10, 2025
2a0b1b1
fix: improve type-stability of `compose`
AayushSabharwal Oct 10, 2025
f48b383
fix: improve type-stability of `split_system`
AayushSabharwal Oct 10, 2025
0ced195
refactor: handle new `reorder_parameters` in `build_explicit_observed…
AayushSabharwal Oct 10, 2025
f5d0bbe
fixup! fix: improve type-stability of connection infrastructure
AayushSabharwal Oct 10, 2025
a795669
fixup! fix: make some SII impls of `IndexCache` more type-stable
AayushSabharwal Oct 10, 2025
cf8af8b
fixup! fix: make `reorder_parameters` more type-stable
AayushSabharwal Oct 10, 2025
89468bf
fixup! fix: improve precompile-friendliness of `complete`
AayushSabharwal Oct 10, 2025
c0f7b91
fixup! fix: make `reorder_parameters` more type-stable
AayushSabharwal Oct 10, 2025
9443322
fix: handle new `reorder_parameters` in `get_mtkparameters_reconstruc…
AayushSabharwal Oct 10, 2025
1545218
refactor: avoid using `vars!`
AayushSabharwal Oct 10, 2025
a67adf2
feat: handle `Const` variants in problem building
AayushSabharwal Oct 10, 2025
8e2b964
refactor: simplify method definitions
AayushSabharwal Oct 10, 2025
3955abb
refactor: improve type-stability of `ShiftIndex`
AayushSabharwal Oct 10, 2025
4ed662b
fixup! fix: make `input_timedomain` type-stable
AayushSabharwal Oct 10, 2025
54bedf5
refactor: improve performance, type stability of `is_bound`
AayushSabharwal Oct 10, 2025
b7f1011
refactor: improve type-stability of `generate_control_function`
AayushSabharwal Oct 10, 2025
7e26935
fixup! fix: make `isparameter` type stable
AayushSabharwal Oct 10, 2025
954af3e
fixup! fix: remove usages of `occursin` for searching expressions
AayushSabharwal Oct 10, 2025
ad3a834
fixup! refactor: get `System` to precompile in a trivial case
AayushSabharwal Oct 10, 2025
f372c4d
refactor: improve type-stability of `check_operator_variables`
AayushSabharwal Oct 10, 2025
288cb10
fixup! fix: improve inference of several utility functions
AayushSabharwal Oct 10, 2025
2c83233
fixup! fix: improve inference of several utility functions
AayushSabharwal Oct 10, 2025
90f1320
fix: improve type-stability of `observed_equations_used_by`
AayushSabharwal Oct 10, 2025
352e5fc
fixup! fix: make `flatten_equations` type-stable
AayushSabharwal Oct 10, 2025
3d05173
fixup! refactor: remove usages of `Symbolics.getparent`
AayushSabharwal Oct 10, 2025
d8c1e58
fixup! fix: improve inference of several utility functions
AayushSabharwal Oct 10, 2025
21fcf5e
fixup! fix: move ChainRulesCore to an extension
AayushSabharwal Oct 13, 2025
91b7c29
fixup! fix: make `add_initialization_parameters` type-stable
AayushSabharwal Oct 13, 2025
3330699
fixup! fix: make `IndexCache` constructor more type-stable
AayushSabharwal Oct 13, 2025
38b72ad
fixup! fix: minor fix for `evaluate_varmap!`
AayushSabharwal Oct 13, 2025
3c2af16
fixup! refactor: get `System` to precompile in a trivial case
AayushSabharwal Oct 13, 2025
d9a4539
fixup! refactor: make `System` more concretely typed
AayushSabharwal Oct 13, 2025
4db99bb
refactor: improve type-stability of constraint validation
AayushSabharwal Oct 13, 2025
db3a50c
fixup! refactor: remove usages of old `symtype` syntax
AayushSabharwal Oct 13, 2025
3e79363
fixup! refactor: concretely type some utility functions
AayushSabharwal Oct 13, 2025
18caee9
fixup! fix: make some SII impls of `IndexCache` more type-stable
AayushSabharwal Oct 13, 2025
058024c
refactor: remove Unitful
AayushSabharwal Oct 13, 2025
240119d
fix: handle consts in `find_eq_solvables!`
AayushSabharwal Oct 14, 2025
bc517bf
refactor: unwrap `AnalysisPoint` inside consts where necessary
AayushSabharwal Oct 14, 2025
e408e06
refactor: make `AnalysisPoint` more type-stable
AayushSabharwal Oct 14, 2025
13bdba9
fixup! fix: improve type-stability of connection infrastructure
AayushSabharwal Oct 14, 2025
bdf423e
fix: fix parameter default parsing in `@mtkmodel`
AayushSabharwal Oct 14, 2025
81681e1
fixup! refactor: get `System` to precompile in a trivial case
AayushSabharwal Oct 14, 2025
0a9ad57
fix: handle symbolic consts in unit checking
AayushSabharwal Oct 14, 2025
9a3a0d6
fixup! fixup! fix: make `flatten_equations` type-stable
AayushSabharwal Oct 14, 2025
83faba1
fix: handle wrapped constants in `dummy_derivative_graph`
AayushSabharwal Oct 14, 2025
6804adb
fix: better handle absent parameter derivatives in simplification
AayushSabharwal Oct 14, 2025
89f1803
fixup! fix: improve type-stability of `tearing_reassemble`
AayushSabharwal Oct 14, 2025
2883d73
fix: handle wrapped constants in `find_eq_solvables!`
AayushSabharwal Oct 14, 2025
8328947
fix: minor bug fix in initsys generation
AayushSabharwal Oct 14, 2025
ed1895c
refactor: avoid using `vars!` in initsys generation
AayushSabharwal Oct 14, 2025
af94889
fix: unwrap symbolic constants in initsys generation
AayushSabharwal Oct 14, 2025
f1b8c49
fixup! fix: improve type-stability of some SII functions
AayushSabharwal Oct 14, 2025
796899e
fixup! fix: improve type-stability of alias elimination
AayushSabharwal Oct 14, 2025
a171233
fixup! fix: fix parameter default parsing in `@mtkmodel`
AayushSabharwal Oct 14, 2025
f4ae3a0
fix: add edge case for unit handling in `@mtkmodel`
AayushSabharwal Oct 14, 2025
0e54dde
fix: handle new `guesses` type in initprob generation
AayushSabharwal Oct 14, 2025
65c2e46
fix: improve type-stability of `System` constructor
AayushSabharwal Oct 14, 2025
7a59416
fix: properly hashcons constant global symbolics
AayushSabharwal Oct 14, 2025
0b4000c
fix: improve type-stability of `subexpressions_not_involving_vars!`
AayushSabharwal Oct 14, 2025
6595ace
fix: handle `Shift` applied to `Equation`
AayushSabharwal Oct 14, 2025
f718670
refactor: reorganize imports to recompile invalidations
AayushSabharwal Oct 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
CommonSolve = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2"
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Expand All @@ -33,7 +32,6 @@ 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"
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
Expand Down Expand Up @@ -68,18 +66,22 @@ 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"
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800"
Pyomo = "0e8e1daf-01b5-4eba-a626-3897743a3816"

[extensions]
MTKBifurcationKitExt = "BifurcationKit"
MTKCasADiDynamicOptExt = "CasADi"
MTKChainRulesCoreExt = "ChainRulesCore"
MTKDeepDiffsExt = "DeepDiffs"
MTKFMIExt = "FMI"
MTKInfiniteOptExt = "InfiniteOpt"
MTKJuliaFormatterExt = "JuliaFormatter"
MTKLabelledArraysExt = "LabelledArrays"
MTKPyomoDynamicOptExt = "Pyomo"

Expand Down
2 changes: 0 additions & 2 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,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"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[compat]
Attractors = "1.24"
Expand Down Expand Up @@ -65,4 +64,3 @@ StochasticDiffEq = "6"
SymbolicIndexingInterface = "0.3.1"
SymbolicUtils = "3, 4"
Symbolics = "6"
Unitful = "1.12"
2 changes: 1 addition & 1 deletion docs/src/API/codegen.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ ModelingToolkit.calculate_A_b
All code generation eventually calls `build_function_wrapper`.

```@docs
build_function_wrapper
ModelingToolkit.build_function_wrapper
```
2 changes: 0 additions & 2 deletions docs/src/API/model_building.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ ModelingToolkit.t_nounits
ModelingToolkit.D_nounits
ModelingToolkit.t
ModelingToolkit.D
ModelingToolkit.t_unitful
ModelingToolkit.D_unitful
```

Users are recommended to use the appropriate common definition in their models. The required
Expand Down
4 changes: 1 addition & 3 deletions docs/src/basics/FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,7 @@ p, replace, alias = SciMLStructures.canonicalize(Tunable(), prob.p)
# changes to the array will be reflected in parameter values
```

See the [basic example on optimizing](https://docs.sciml.ai/ModelingToolkit/dev/examples/remake/#Optimizing-through-an-ODE-solve-and-re-creating-MTK-Problems) for combining these steps to optimizing parameters and use ForwardDiff.jl as the backend for Automatic Differentiation.

# ERROR: ArgumentError: SymbolicUtils.BasicSymbolic{Real}[xˍt(t)] are missing from the variable map.
# ERROR: ArgumentError: `[xˍt(t)]` 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.

Expand Down
4 changes: 2 additions & 2 deletions docs/src/basics/Validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ future when `ModelingToolkit` is extended to support eliminating `DynamicQuantit

## Other Restrictions

`Unitful` provides non-scalar units such as `dBm`, `°C`, etc. At this time, `ModelingToolkit` only supports scalar quantities. Additionally, angular degrees (`°`) are not supported because trigonometric functions will treat plain numerical values as radians, which would lead systems validated using degrees to behave erroneously when being solved.
`DynamicQuantities` provides non-scalar units such as `°C`, etc. At this time, `ModelingToolkit` only supports scalar quantities. Additionally, angular degrees (`°`) are not supported because trigonometric functions will treat plain numerical values as radians, which would lead systems validated using degrees to behave erroneously when being solved.

## Troubleshooting & Gotchas

Expand All @@ -169,7 +169,7 @@ Parameter and initial condition values are supplied to problem constructors as p

```julia
function remove_units(p::Dict)
Dict(k => Unitful.ustrip(ModelingToolkit.get_unit(k), v) for (k, v) in p)
Dict(k => DynamicQuantities.ustrip(ModelingToolkit.get_unit(k), v) for (k, v) in p)
end
add_units(p::Dict) = Dict(k => v * ModelingToolkit.get_unit(k) for (k, v) in p)
```
Expand Down
2 changes: 1 addition & 1 deletion ext/MTKCasADiDynamicOptExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ end

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]
t isa Union{Num, SymbolicT} ? X.u[i, :] : X(t)[i]
end

function MTK.lowered_integral(model::CasADiModel, expr, lo, hi)
Expand Down
19 changes: 19 additions & 0 deletions src/adjoints.jl → ext/MTKChainRulesCoreExt.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
module MTKChainRulesCoreExt

import ChainRulesCore
import ChainRulesCore: Tangent, ZeroTangent, NoTangent, zero_tangent, unthunk
using ModelingToolkit: MTKParameters, NONNUMERIC_PORTION, AbstractSystem
import ModelingToolkit
import ModelingToolkit as MTK
import SciMLStructures
import SymbolicIndexingInterface: remake_buffer
import SciMLBase: AbstractNonlinearProblem, remake

function ChainRulesCore.rrule(::Type{MTKParameters}, tunables, args...)
function mtp_pullback(dt)
dt = unthunk(dt)
Expand Down Expand Up @@ -104,3 +115,11 @@ function ChainRulesCore.rrule(
end

ChainRulesCore.@non_differentiable Base.getproperty(sys::AbstractSystem, x::Symbol)

function ModelingToolkit.update_initializeprob!(initprob::AbstractNonlinearProblem, prob)
pgetter = ChainRulesCore.@ignore_derivatives MTK.get_scimlfn(prob).initialization_data.metadata.oop_reconstruct_u0_p.pgetter
p = pgetter(prob, initprob)
return remake(initprob; p)
end

end
6 changes: 3 additions & 3 deletions ext/MTKInfiniteOptExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ end

function MTK.lowered_var(m::InfiniteOptModel, uv, i, t)
X = getfield(m, uv)
t isa Union{Num, Symbolics.Symbolic} ? X[i] : X[i](t)
t isa Union{Num, SymbolicT} ? X[i] : X[i](t)
end

function add_solve_constraints!(prob::JuMPDynamicOptProblem, tableau)
Expand Down Expand Up @@ -256,13 +256,13 @@ for ff in [acos, log1p, acosh, log2, asin, tan, atanh, cos, log, sin, log10, sqr
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.
function Base.isequal(::SymbolicUtils.Symbolic,
function Base.isequal(::SymbolicT,
::Union{JuMP.GenericAffExpr, JuMP.GenericQuadExpr, InfiniteOpt.AbstractInfOptExpr})
false
end
function Base.isequal(
::Union{JuMP.GenericAffExpr, JuMP.GenericQuadExpr, InfiniteOpt.AbstractInfOptExpr},
::SymbolicUtils.Symbolic)
::SymbolicT)
false
end
end
12 changes: 12 additions & 0 deletions ext/MTKJuliaFormatterExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module MTKJuliaFormatterExt

import ModelingToolkit: readable_code, _readable_code, rec_remove_macro_linenums!
import JuliaFormatter

function readable_code(expr::Expr)
expr = Base.remove_linenums!(_readable_code(expr))
rec_remove_macro_linenums!(expr)
JuliaFormatter.format_text(string(expr), JuliaFormatter.SciMLStyle())
end

end
9 changes: 5 additions & 4 deletions ext/MTKPyomoDynamicOptExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ using DiffEqBase
using UnPack
using NaNMath
using Setfield
import SymbolicUtils as SU
const MTK = ModelingToolkit

const SPECIAL_FUNCTIONS_DICT = Dict([acos => Pyomo.py_acos,
Expand Down Expand Up @@ -53,7 +54,7 @@ struct PyomoDynamicOptProblem{uType, tType, isinplace, P, F, K} <:
end
end

function pysym_getproperty(s::Union{Num, Symbolics.Symbolic}, name::Symbol)
function pysym_getproperty(s::Union{Num, SymbolicT}, name::Symbol)
Symbolics.wrap(SymbolicUtils.term(
_getproperty, Symbolics.unwrap(s), Val{name}(), type = Symbolics.Struct{PyomoVar}))
end
Expand Down Expand Up @@ -112,7 +113,7 @@ function MTK.add_constraint!(pmodel::PyomoDynamicOptModel, cons; n_idxs = 1)
Symbolics.unwrap(expr), SPECIAL_FUNCTIONS_DICT, fold = false)

cons_sym = Symbol("cons", hash(cons))
if occursin(Symbolics.unwrap(t_sym), expr)
if SU.query(isequal(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
Expand All @@ -124,7 +125,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, fold = false)
if occursin(Symbolics.unwrap(t_sym), expr)
if SU.query(isequal(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
Expand Down Expand Up @@ -165,7 +166,7 @@ end

function MTK.lowered_var(m::PyomoDynamicOptModel, uv, i, t)
X = Symbolics.value(pysym_getproperty(m.model_sym, uv))
var = t isa Union{Num, Symbolics.Symbolic} ? X[i, m.t_sym] : X[i, t]
var = t isa Union{Num, SymbolicT} ? X[i, m.t_sym] : X[i, t]
Symbolics.unwrap(var)
end

Expand Down
Loading