Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
51fa305
refactor: remove usages of `Symbolic`
AayushSabharwal Sep 30, 2025
e933777
refactor: remove usages of `_parse_vars`
AayushSabharwal Sep 30, 2025
4ca242b
refactor: remove outdated imports
AayushSabharwal Sep 30, 2025
cf100b0
refactor: remove usages of `fast_substitute`
AayushSabharwal Sep 30, 2025
ad938e2
refactor: remove usages of `CallWithMetadata`
AayushSabharwal Sep 30, 2025
a421336
refactor: remove usages of old `symtype` syntax
AayushSabharwal Sep 30, 2025
6e3bb48
refactor: remove usages of `Difference`
AayushSabharwal Sep 30, 2025
06c78b0
refactor: remove usages of `Symbolics._mapreduce`
AayushSabharwal Sep 30, 2025
63a6a39
refactor: remove usages of `Symbolics.getparent`
AayushSabharwal Sep 30, 2025
d3f1908
fix: implement `SII.getname` for `System`
AayushSabharwal Sep 30, 2025
28cb220
fix: remove usages of `Symbolics.Unknown`, `Symbolics.shape`
AayushSabharwal Sep 30, 2025
a4cf26e
refactor: remove usages of `array_term`
AayushSabharwal Sep 30, 2025
3410078
fix: move ChainRulesCore to an extension
AayushSabharwal Oct 1, 2025
ffec46c
fix: move JuliaFormatter to an extension
AayushSabharwal Oct 1, 2025
19b3f5c
fix: fix invalidations from `promote_symtype` method
AayushSabharwal Oct 1, 2025
7b49807
fix: remove usages of `occursin` for searching expressions
AayushSabharwal Oct 1, 2025
56d0d39
fix: remove usages of deprecated `children`
AayushSabharwal Oct 1, 2025
f8d20ad
fix: remove usages of deprecated DataStructures features
AayushSabharwal Oct 1, 2025
b5f1b6d
fixup! refactor: remove usages of old `symtype` syntax
AayushSabharwal Oct 1, 2025
7734445
fixup! refactor: remove usages of old `symtype` syntax
AayushSabharwal Oct 1, 2025
3e9aab2
refactor: concretely type some utility functions
AayushSabharwal Oct 1, 2025
5732ce3
fix: handle `Const` in connection expansion
AayushSabharwal Oct 1, 2025
a33a721
fixup! refactor: remove usages of `array_term`
AayushSabharwal Oct 1, 2025
8393cb7
refactor: improve type-stability of `renamespace`
AayushSabharwal Oct 1, 2025
587476c
refactor: make `System` more concretely typed
AayushSabharwal Oct 1, 2025
f79523b
fixup! refactor: remove usages of `CallWithMetadata`
AayushSabharwal Oct 1, 2025
0689b54
fix: fix usage of new `get_variables` in `is_bound`
AayushSabharwal Oct 1, 2025
1dd83b5
fixup! refactor: remove usages of `array_term`
AayushSabharwal Oct 1, 2025
d2a74a0
fix: handle removal of `operator` kwarg of `substitute`
AayushSabharwal Oct 1, 2025
5cad99f
refactor: get `System` to precompile in a trivial case
AayushSabharwal Oct 1, 2025
28c3542
fixup! fix: fix invalidations from `promote_symtype` method
AayushSabharwal Oct 1, 2025
8f1493f
fixup! refactor: get `System` to precompile in a trivial case
AayushSabharwal Oct 1, 2025
692928b
fix: improve inference of variable metadata accessors
AayushSabharwal Oct 2, 2025
82eb4ba
fix: make `default_toterm` type stable
AayushSabharwal Oct 2, 2025
f7231ba
fix: improve inference of several utility functions
AayushSabharwal Oct 2, 2025
b30bcc8
fix: make `shift2term` type-stable
AayushSabharwal Oct 2, 2025
5b47bea
fix: make `unhack_observed` type-stable
AayushSabharwal Oct 2, 2025
b921da8
fix: make `independent_variables` type-stable
AayushSabharwal Oct 2, 2025
9161395
fix: improve type-stability of some SII functions
AayushSabharwal Oct 2, 2025
badfb9f
fix: improve type-stability of the `Initial` operator
AayushSabharwal Oct 2, 2025
41ca68b
fix: make `add_initialization_parameters` type-stable
AayushSabharwal Oct 2, 2025
cef5f24
fix: improve type-stability of `discover_globalscoped`
AayushSabharwal Oct 2, 2025
a6ba3cd
fix: make topsorting equations type-stable
AayushSabharwal Oct 2, 2025
27e1ec4
fix: make `SymbolicAffect` and `AffectSystem` type-stable
AayushSabharwal Oct 2, 2025
9fa324b
fix: improve type-stability of `ImperativeAffect`
AayushSabharwal Oct 2, 2025
a2d9efa
fix: make `reorder_parameters` more type-stable
AayushSabharwal Oct 2, 2025
6b22f32
fix: make some SII impls of `IndexCache` more type-stable
AayushSabharwal Oct 2, 2025
33d5fc3
fix: make `IndexCache` constructor more type-stable
AayushSabharwal Oct 2, 2025
9b5fee9
fix: improve precompile-friendliness of `complete`
AayushSabharwal Oct 2, 2025
01a2b9a
refactor: miscellaneous improvements
AayushSabharwal Oct 2, 2025
bf11961
fix: minor fix for `evaluate_varmap!`
AayushSabharwal Oct 2, 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: 1 addition & 1 deletion docs/src/basics/FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ p, replace, alias = SciMLStructures.canonicalize(Tunable(), prob.p)
# changes to the array will be reflected in parameter values
```

# 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
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
18 changes: 18 additions & 0 deletions src/adjoints.jl → ext/MTKChainRulesCoreExt.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
module MTKChainRulesCoreExt

import ChainRulesCore
import ChainRulesCore: Tangent, ZeroTangent, NoTangent, zero_tangent, unthunk
using ModelingToolkit: MTKParameters, NONNUMERIC_PORTION, AbstractSystem
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 +114,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
8 changes: 4 additions & 4 deletions ext/MTKPyomoDynamicOptExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,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 +112,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 +124,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 +165,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
141 changes: 123 additions & 18 deletions src/ModelingToolkit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ using PrecompileTools, Reexport
end

import SymbolicUtils
import SymbolicUtils as SU
import SymbolicUtils: iscall, arguments, operation, maketerm, promote_symtype,
Symbolic, isadd, ismul, ispow, issym, FnType,
@rule, Rewriters, substitute, metadata, BasicSymbolic,
Sym, Term
isadd, ismul, ispow, issym, FnType, isconst, BSImpl,
@rule, Rewriters, substitute, metadata, BasicSymbolic
using SymbolicUtils.Code
import SymbolicUtils.Code: toexpr
import SymbolicUtils.Rewriters: Chain, Postwalk, Prewalk, Fixpoint
using DocStringExtensions
using SpecialFunctions, NaNMath
using DiffEqCallbacks
@recompile_invalidations begin
using DiffEqCallbacks
end
using Graphs
import ExprTools: splitdef, combinedef
import OrderedCollections
Expand Down Expand Up @@ -48,7 +50,6 @@ using SciMLBase: StandardODEProblem, StandardNonlinearProblem, handle_varmap, Ti
PeriodicClock, Clock, SolverStepClock, ContinuousClock, OverrideInit,
NoInit
using Distributed
import JuliaFormatter
using MLStyle
import Moshi
using Moshi.Data: @data
Expand All @@ -62,19 +63,16 @@ 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

using Symbolics: degree
using Symbolics: _parse_vars, value, @derivatives, get_variables,
exprs_occur_in, symbolic_linear_solve, build_expr, unwrap, wrap,
using Symbolics: degree, VartypeT, SymbolicT
using Symbolics: parse_vars, value, @derivatives, get_variables,
exprs_occur_in, symbolic_linear_solve, unwrap, wrap,
VariableSource, getname, variable,
NAMESPACE_SEPARATOR, set_scalar_metadata, setdefaultval,
hasnode, fixpoint_sub, fast_substitute,
CallWithMetadata, CallWithParent
NAMESPACE_SEPARATOR, setdefaultval,
hasnode, fixpoint_sub, CallAndWrap, SArgsT, SSym, STerm
const NAMESPACE_SEPARATOR_SYMBOL = Symbol(NAMESPACE_SEPARATOR)
import Symbolics: rename, get_variables!, _solve, hessian_sparsity,
jacobian_sparsity, isaffine, islinear, _iszero, _isone,
Expand All @@ -83,7 +81,7 @@ import Symbolics: rename, get_variables!, _solve, hessian_sparsity,
ParallelForm, SerialForm, MultithreadedForm, build_function,
rhss, lhss, prettify_expr, gradient,
jacobian, hessian, derivative, sparsejacobian, sparsehessian,
substituter, scalarize, getparent, hasderiv, hasdiff
scalarize, hasderiv

import DiffEqBase: @add_kwonly
export independent_variables, unknowns, observables, parameters, full_parameters,
Expand Down Expand Up @@ -157,6 +155,9 @@ include("parameters.jl")
include("independent_variables.jl")
include("constants.jl")

const SymmapT = Dict{SymbolicT, SymbolicT}
const COMMON_NOTHING = SU.Const{VartypeT}(nothing)

include("utils.jl")

include("systems/index_cache.jl")
Expand Down Expand Up @@ -225,7 +226,6 @@ include("structural_transformation/StructuralTransformations.jl")
@reexport using .StructuralTransformations
include("inputoutput.jl")

include("adjoints.jl")
include("deprecations.jl")

const t_nounits = let
Expand Down Expand Up @@ -334,6 +334,8 @@ export AbstractCollocation, JuMPCollocation, InfiniteOptCollocation,
CasADiCollocation, PyomoCollocation
export DynamicOptSolution

const set_scalar_metadata = setmetadata

@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
Expand All @@ -356,10 +358,100 @@ for prop in [SYS_PROPS; [:continuous_events, :discrete_events]]
end

PrecompileTools.@compile_workload begin
using ModelingToolkit
fold1 = Val{false}()
using SymbolicUtils
using SymbolicUtils: shape
using Symbolics
@syms x y f(t) q[1:5]
SymbolicUtils.Sym{SymReal}(:a; type = Real, shape = SymbolicUtils.ShapeVecT())
x + y
x * y
x / y
x ^ y
x ^ 5
6 ^ x
x - y
-y
2y
z = 2
dict = SymbolicUtils.ACDict{VartypeT}()
dict[x] = 1
dict[y] = 1
type::typeof(DataType) = rand() < 0.5 ? Real : Float64
nt = (; type, shape, unsafe = true)
Base.pairs(nt)
BSImpl.AddMul{VartypeT}(1, dict, SymbolicUtils.AddMulVariant.MUL; type, shape = SymbolicUtils.ShapeVecT(), unsafe = true)
*(y, z)
*(z, y)
SymbolicUtils.symtype(y)
f(x)
(5x / 5)
expand((x + y) ^ 2)
simplify(x ^ (1//2) + (sin(x) ^ 2 + cos(x) ^ 2) + 2(x + y) - x - y)
ex = x + 2y + sin(x)
rules1 = Dict(x => y)
rules2 = Dict(x => 1)
Dx = Differential(x)
Differential(y)(ex)
uex = unwrap(ex)
Symbolics.executediff(Dx, uex)
# Running `fold = Val(true)` invalidates the precompiled statements
# for `fold = Val(false)` and itself doesn't precompile anyway.
# substitute(ex, rules1)
substitute(ex, rules1; fold = fold1)
substitute(ex, rules2; fold = fold1)
@variables foo
f(foo)
@variables x y f(::Real) q[1:5]
x + y
x * y
x / y
x ^ y
x ^ 5
# 6 ^ x
x - y
-y
2y
symtype(y)
z = 2
*(y, z)
*(z, y)
f(x)
(5x / 5)
[x, y]
[x, f, f]
promote_type(Int, Num)
promote_type(Real, Num)
promote_type(Float64, Num)
# expand((x + y) ^ 2)
# simplify(x ^ (1//2) + (sin(x) ^ 2 + cos(x) ^ 2) + 2(x + y) - x - y)
ex = x + 2y + sin(x)
rules1 = Dict(x => y)
# rules2 = Dict(x => 1)
# Running `fold = Val(true)` invalidates the precompiled statements
# for `fold = Val(false)` and itself doesn't precompile anyway.
# substitute(ex, rules1)
substitute(ex, rules1; fold = fold1)
Symbolics.linear_expansion(ex, y)
# substitute(ex, rules2; fold = fold1)
# substitute(ex, rules2)
# substitute(ex, rules1; fold = fold2)
# substitute(ex, rules2; fold = fold2)
q[1]
q'q
using ModelingToolkit
Comment on lines +361 to +442
Copy link
Contributor

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
fold1 = Val{false}()
using SymbolicUtils
using SymbolicUtils: shape
using Symbolics
@syms x y f(t) q[1:5]
SymbolicUtils.Sym{SymReal}(:a; type = Real, shape = SymbolicUtils.ShapeVecT())
x + y
x * y
x / y
x ^ y
x ^ 5
6 ^ x
x - y
-y
2y
z = 2
dict = SymbolicUtils.ACDict{VartypeT}()
dict[x] = 1
dict[y] = 1
type::typeof(DataType) = rand() < 0.5 ? Real : Float64
nt = (; type, shape, unsafe = true)
Base.pairs(nt)
BSImpl.AddMul{VartypeT}(1, dict, SymbolicUtils.AddMulVariant.MUL; type, shape = SymbolicUtils.ShapeVecT(), unsafe = true)
*(y, z)
*(z, y)
SymbolicUtils.symtype(y)
f(x)
(5x / 5)
expand((x + y) ^ 2)
simplify(x ^ (1//2) + (sin(x) ^ 2 + cos(x) ^ 2) + 2(x + y) - x - y)
ex = x + 2y + sin(x)
rules1 = Dict(x => y)
rules2 = Dict(x => 1)
Dx = Differential(x)
Differential(y)(ex)
uex = unwrap(ex)
Symbolics.executediff(Dx, uex)
# Running `fold = Val(true)` invalidates the precompiled statements
# for `fold = Val(false)` and itself doesn't precompile anyway.
# substitute(ex, rules1)
substitute(ex, rules1; fold = fold1)
substitute(ex, rules2; fold = fold1)
@variables foo
f(foo)
@variables x y f(::Real) q[1:5]
x + y
x * y
x / y
x ^ y
x ^ 5
# 6 ^ x
x - y
-y
2y
symtype(y)
z = 2
*(y, z)
*(z, y)
f(x)
(5x / 5)
[x, y]
[x, f, f]
promote_type(Int, Num)
promote_type(Real, Num)
promote_type(Float64, Num)
# expand((x + y) ^ 2)
# simplify(x ^ (1//2) + (sin(x) ^ 2 + cos(x) ^ 2) + 2(x + y) - x - y)
ex = x + 2y + sin(x)
rules1 = Dict(x => y)
# rules2 = Dict(x => 1)
# Running `fold = Val(true)` invalidates the precompiled statements
# for `fold = Val(false)` and itself doesn't precompile anyway.
# substitute(ex, rules1)
substitute(ex, rules1; fold = fold1)
Symbolics.linear_expansion(ex, y)
# substitute(ex, rules2; fold = fold1)
# substitute(ex, rules2)
# substitute(ex, rules1; fold = fold2)
# substitute(ex, rules2; fold = fold2)
q[1]
q'q
using ModelingToolkit
fold1 = Val{false}()
using SymbolicUtils
using SymbolicUtils: shape
using Symbolics
@syms x y f(t) q[1:5]
SymbolicUtils.Sym{SymReal}(:a; type = Real, shape = SymbolicUtils.ShapeVecT())
x + y
x * y
x / y
x ^ y
x ^ 5
6 ^ x
x - y
-y
2y
z = 2
dict = SymbolicUtils.ACDict{VartypeT}()
dict[x] = 1
dict[y] = 1
type::typeof(DataType) = rand() < 0.5 ? Real : Float64
nt = (; type, shape, unsafe = true)
Base.pairs(nt)
BSImpl.AddMul{VartypeT}(1, dict, SymbolicUtils.AddMulVariant.MUL; type,
shape = SymbolicUtils.ShapeVecT(), unsafe = true)
*(y, z)
*(z, y)
SymbolicUtils.symtype(y)
f(x)
(5x / 5)
expand((x + y) ^ 2)
simplify(x ^ (1//2) + (sin(x) ^ 2 + cos(x) ^ 2) + 2(x + y) - x - y)
ex = x + 2y + sin(x)
rules1 = Dict(x => y)
rules2 = Dict(x => 1)
Dx = Differential(x)
Differential(y)(ex)
uex = unwrap(ex)
Symbolics.executediff(Dx, uex)
# Running `fold = Val(true)` invalidates the precompiled statements
# for `fold = Val(false)` and itself doesn't precompile anyway.
# substitute(ex, rules1)
substitute(ex, rules1; fold = fold1)
substitute(ex, rules2; fold = fold1)
@variables foo
f(foo)
@variables x y f(::Real) q[1:5]
x + y
x * y
x / y
x ^ y
x ^ 5
# 6 ^ x
x - y
-y
2y
symtype(y)
z = 2
*(y, z)
*(z, y)
f(x)
(5x / 5)
[x, y]
[x, f, f]
promote_type(Int, Num)
promote_type(Real, Num)
promote_type(Float64, Num)
# expand((x + y) ^ 2)
# simplify(x ^ (1//2) + (sin(x) ^ 2 + cos(x) ^ 2) + 2(x + y) - x - y)
ex = x + 2y + sin(x)
rules1 = Dict(x => y)
# rules2 = Dict(x => 1)
# Running `fold = Val(true)` invalidates the precompiled statements
# for `fold = Val(false)` and itself doesn't precompile anyway.
# substitute(ex, rules1)
substitute(ex, rules1; fold = fold1)
Symbolics.linear_expansion(ex, y)
# substitute(ex, rules2; fold = fold1)
# substitute(ex, rules2)
# substitute(ex, rules1; fold = fold2)
# substitute(ex, rules2; fold = fold2)
q[1]
q'q
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)
isequal(ModelingToolkit.D_nounits.x, ModelingToolkit.t_nounits)
sys = System([ModelingToolkit.D_nounits(x) ~ x], ModelingToolkit.t_nounits, [x], Num[]; name = :sys)
Copy link
Contributor

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
sys = System([ModelingToolkit.D_nounits(x) ~ x], ModelingToolkit.t_nounits, [x], Num[]; name = :sys)
sys = System([ModelingToolkit.D_nounits(x) ~ x],
ModelingToolkit.t_nounits, [x], Num[]; name = :sys)

complete(sys)
@syms p[1:2]
ndims(p)
size(p)
axes(p)
length(p)
v = [p]
isempty(v)
# mtkcompile(sys)
@mtkmodel __testmod__ begin
@constants begin
c = 1.0
Expand Down Expand Up @@ -390,4 +482,17 @@ PrecompileTools.@compile_workload begin
end
end

precompile(Tuple{typeof(Base.merge), NamedTuple{(:f, :args, :metadata, :hash, :hash2, :shape, :type, :id), Tuple{SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal}, SymbolicUtils.SmallVec{SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal}, Array{SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal}, 1}}, Nothing, UInt64, UInt64, SymbolicUtils.SmallVec{Base.UnitRange{Int64}, Array{Base.UnitRange{Int64}, 1}}, DataType, SymbolicUtils.IDType}}, NamedTuple{(:metadata,), Tuple{Base.ImmutableDict{DataType, Any}}}})
precompile(Tuple{typeof(Base.merge), NamedTuple{(:f, :args, :metadata, :hash, :hash2, :shape, :type, :id), Tuple{SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal}, SymbolicUtils.SmallVec{SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal}, Array{SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal}, 1}}, Base.ImmutableDict{DataType, Any}, UInt64, UInt64, SymbolicUtils.SmallVec{Base.UnitRange{Int64}, Array{Base.UnitRange{Int64}, 1}}, DataType, SymbolicUtils.IDType}}, NamedTuple{(:id, :hash, :hash2), Tuple{Nothing, Int64, Int64}}})
precompile(Tuple{typeof(Core.kwcall), NamedTuple{(:f, :args, :metadata, :hash, :hash2, :shape, :type, :id), Tuple{SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal}, SymbolicUtils.SmallVec{SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal}, Array{SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal}, 1}}, Base.ImmutableDict{DataType, Any}, Int64, Int64, SymbolicUtils.SmallVec{Base.UnitRange{Int64}, Array{Base.UnitRange{Int64}, 1}}, DataType, Nothing}}, Type{SymbolicUtils.BasicSymbolicImpl.Term{SymbolicUtils.SymReal}}})
precompile(Tuple{typeof(Symbolics.parse_vars), Symbol, Type, Tuple{Symbol, Symbol}, Function})
precompile(Tuple{typeof(Base.merge), NamedTuple{(:name, :metadata, :hash, :hash2, :shape, :type, :id), Tuple{Symbol, Base.ImmutableDict{DataType, Any}, UInt64, UInt64, SymbolicUtils.SmallVec{Base.UnitRange{Int64}, Array{Base.UnitRange{Int64}, 1}}, DataType, SymbolicUtils.IDType}}, NamedTuple{(:metadata,), Tuple{Base.ImmutableDict{DataType, Any}}}})
Comment on lines +485 to +489
Copy link
Contributor

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
precompile(Tuple{typeof(Base.merge), NamedTuple{(:f, :args, :metadata, :hash, :hash2, :shape, :type, :id), Tuple{SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal}, SymbolicUtils.SmallVec{SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal}, Array{SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal}, 1}}, Nothing, UInt64, UInt64, SymbolicUtils.SmallVec{Base.UnitRange{Int64}, Array{Base.UnitRange{Int64}, 1}}, DataType, SymbolicUtils.IDType}}, NamedTuple{(:metadata,), Tuple{Base.ImmutableDict{DataType, Any}}}})
precompile(Tuple{typeof(Base.merge), NamedTuple{(:f, :args, :metadata, :hash, :hash2, :shape, :type, :id), Tuple{SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal}, SymbolicUtils.SmallVec{SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal}, Array{SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal}, 1}}, Base.ImmutableDict{DataType, Any}, UInt64, UInt64, SymbolicUtils.SmallVec{Base.UnitRange{Int64}, Array{Base.UnitRange{Int64}, 1}}, DataType, SymbolicUtils.IDType}}, NamedTuple{(:id, :hash, :hash2), Tuple{Nothing, Int64, Int64}}})
precompile(Tuple{typeof(Core.kwcall), NamedTuple{(:f, :args, :metadata, :hash, :hash2, :shape, :type, :id), Tuple{SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal}, SymbolicUtils.SmallVec{SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal}, Array{SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal}, 1}}, Base.ImmutableDict{DataType, Any}, Int64, Int64, SymbolicUtils.SmallVec{Base.UnitRange{Int64}, Array{Base.UnitRange{Int64}, 1}}, DataType, Nothing}}, Type{SymbolicUtils.BasicSymbolicImpl.Term{SymbolicUtils.SymReal}}})
precompile(Tuple{typeof(Symbolics.parse_vars), Symbol, Type, Tuple{Symbol, Symbol}, Function})
precompile(Tuple{typeof(Base.merge), NamedTuple{(:name, :metadata, :hash, :hash2, :shape, :type, :id), Tuple{Symbol, Base.ImmutableDict{DataType, Any}, UInt64, UInt64, SymbolicUtils.SmallVec{Base.UnitRange{Int64}, Array{Base.UnitRange{Int64}, 1}}, DataType, SymbolicUtils.IDType}}, NamedTuple{(:metadata,), Tuple{Base.ImmutableDict{DataType, Any}}}})
precompile(Tuple{typeof(Base.merge),
NamedTuple{(:f, :args, :metadata, :hash, :hash2, :shape, :type, :id),
Tuple{
SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal},
SymbolicUtils.SmallVec{
SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal},
Array{
SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal},
1}},
Nothing,
UInt64,
UInt64,
SymbolicUtils.SmallVec{Base.UnitRange{Int64}, Array{Base.UnitRange{Int64}, 1}},
DataType,
SymbolicUtils.IDType}},
NamedTuple{(:metadata,), Tuple{Base.ImmutableDict{DataType, Any}}}})
precompile(Tuple{typeof(Base.merge),
NamedTuple{(:f, :args, :metadata, :hash, :hash2, :shape, :type, :id),
Tuple{
SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal},
SymbolicUtils.SmallVec{
SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal},
Array{
SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal},
1}},
Base.ImmutableDict{DataType, Any},
UInt64,
UInt64,
SymbolicUtils.SmallVec{Base.UnitRange{Int64}, Array{Base.UnitRange{Int64}, 1}},
DataType,
SymbolicUtils.IDType}},
NamedTuple{(:id, :hash, :hash2), Tuple{Nothing, Int64, Int64}}})
precompile(Tuple{typeof(Core.kwcall),
NamedTuple{(:f, :args, :metadata, :hash, :hash2, :shape, :type, :id),
Tuple{
SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal},
SymbolicUtils.SmallVec{
SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal},
Array{
SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal},
1}},
Base.ImmutableDict{DataType, Any},
Int64,
Int64,
SymbolicUtils.SmallVec{Base.UnitRange{Int64}, Array{Base.UnitRange{Int64}, 1}},
DataType,
Nothing}},
Type{SymbolicUtils.BasicSymbolicImpl.Term{SymbolicUtils.SymReal}}})
precompile(Tuple{
typeof(Symbolics.parse_vars), Symbol, Type, Tuple{Symbol, Symbol}, Function})
precompile(Tuple{typeof(Base.merge),
NamedTuple{(:name, :metadata, :hash, :hash2, :shape, :type, :id),
Tuple{Symbol, Base.ImmutableDict{DataType, Any}, UInt64, UInt64,
SymbolicUtils.SmallVec{Base.UnitRange{Int64}, Array{Base.UnitRange{Int64}, 1}},
DataType, SymbolicUtils.IDType}},
NamedTuple{(:metadata,), Tuple{Base.ImmutableDict{DataType, Any}}}})

precompile(Tuple{typeof(Base.vect), Symbolics.Equation, Vararg{Symbolics.Equation}})
precompile(Tuple{typeof(Core.kwcall), NamedTuple{(:name, :defaults), Tuple{Symbol, Base.Dict{Symbolics.Num, Float64}}}, Type{ModelingToolkit.System}, Array{Symbolics.Equation, 1}, Symbolics.Num, Array{Symbolics.Num, 1}, Array{Symbolics.Num, 1}})
precompile(Tuple{Type{NamedTuple{(:name, :defaults), T} where T<:Tuple}, Tuple{Symbol, Base.Dict{Symbolics.Num, Float64}}})
Comment on lines +491 to +492
Copy link
Contributor

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
precompile(Tuple{typeof(Core.kwcall), NamedTuple{(:name, :defaults), Tuple{Symbol, Base.Dict{Symbolics.Num, Float64}}}, Type{ModelingToolkit.System}, Array{Symbolics.Equation, 1}, Symbolics.Num, Array{Symbolics.Num, 1}, Array{Symbolics.Num, 1}})
precompile(Tuple{Type{NamedTuple{(:name, :defaults), T} where T<:Tuple}, Tuple{Symbol, Base.Dict{Symbolics.Num, Float64}}})
precompile(Tuple{typeof(Core.kwcall),
NamedTuple{(:name, :defaults), Tuple{Symbol, Base.Dict{Symbolics.Num, Float64}}},
Type{ModelingToolkit.System}, Array{Symbolics.Equation, 1},
Symbolics.Num, Array{Symbolics.Num, 1}, Array{Symbolics.Num, 1}})
precompile(Tuple{Type{NamedTuple{(:name, :defaults), T} where T <: Tuple},
Tuple{Symbol, Base.Dict{Symbolics.Num, Float64}}})

precompile(Tuple{typeof(SymbolicUtils.isequal_somescalar), Float64, Float64})
precompile(Tuple{Type{NamedTuple{(:name, :defaults, :guesses), T} where T<:Tuple}, Tuple{Symbol, Base.Dict{Symbolics.Num, Float64}, Base.Dict{Symbolics.Num, Float64}}})
precompile(Tuple{typeof(Core.kwcall), NamedTuple{(:name, :defaults, :guesses), Tuple{Symbol, Base.Dict{Symbolics.Num, Float64}, Base.Dict{Symbolics.Num, Float64}}}, Type{ModelingToolkit.System}, Array{Symbolics.Equation, 1}, Symbolics.Num, Array{Symbolics.Num, 1}, Array{Symbolics.Num, 1}})
precompile(Tuple{typeof(Core.kwcall), NamedTuple{(:type, :shape), Tuple{DataType, SymbolicUtils.SmallVec{Base.UnitRange{Int64}, Array{Base.UnitRange{Int64}, 1}}}}, typeof(SymbolicUtils.term), Any, SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal}})
Comment on lines +494 to +496
Copy link
Contributor

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
precompile(Tuple{Type{NamedTuple{(:name, :defaults, :guesses), T} where T<:Tuple}, Tuple{Symbol, Base.Dict{Symbolics.Num, Float64}, Base.Dict{Symbolics.Num, Float64}}})
precompile(Tuple{typeof(Core.kwcall), NamedTuple{(:name, :defaults, :guesses), Tuple{Symbol, Base.Dict{Symbolics.Num, Float64}, Base.Dict{Symbolics.Num, Float64}}}, Type{ModelingToolkit.System}, Array{Symbolics.Equation, 1}, Symbolics.Num, Array{Symbolics.Num, 1}, Array{Symbolics.Num, 1}})
precompile(Tuple{typeof(Core.kwcall), NamedTuple{(:type, :shape), Tuple{DataType, SymbolicUtils.SmallVec{Base.UnitRange{Int64}, Array{Base.UnitRange{Int64}, 1}}}}, typeof(SymbolicUtils.term), Any, SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal}})
precompile(Tuple{Type{NamedTuple{(:name, :defaults, :guesses), T} where T <: Tuple},
Tuple{Symbol, Base.Dict{Symbolics.Num, Float64}, Base.Dict{Symbolics.Num, Float64}}})
precompile(Tuple{typeof(Core.kwcall),
NamedTuple{(:name, :defaults, :guesses),
Tuple{Symbol, Base.Dict{Symbolics.Num, Float64}, Base.Dict{Symbolics.Num, Float64}}},
Type{ModelingToolkit.System},
Array{Symbolics.Equation, 1},
Symbolics.Num,
Array{Symbolics.Num, 1},
Array{Symbolics.Num, 1}})
precompile(Tuple{typeof(Core.kwcall),
NamedTuple{(:type, :shape),
Tuple{DataType,
SymbolicUtils.SmallVec{Base.UnitRange{Int64}, Array{Base.UnitRange{Int64}, 1}}}},
typeof(SymbolicUtils.term),
Any,
SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymbolicUtils.SymReal}})


end # module
4 changes: 2 additions & 2 deletions src/clock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,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)
function has_time_domain(x::SymbolicT)
# getmetadata(x, ContinuousClock, nothing) !== nothing ||
# getmetadata(x, Discrete, nothing) !== nothing
getmetadata(x, VariableTimeDomain, nothing) !== nothing
Expand Down Expand Up @@ -77,7 +77,7 @@ See also [`is_continuous_domain`](@ref)
"""
function has_continuous_domain(x)
issym(x) && return is_continuous_domain(x)
hasderiv(x) || hasdiff(x) || hassample(x) || hashold(x)
hasderiv(x) || hassample(x) || hashold(x)
end

"""
Expand Down
Loading
Loading