Skip to content

Commit ba1a7b8

Browse files
authored
Merge branch 'master' into obs_dep
2 parents c127f24 + 47f1547 commit ba1a7b8

25 files changed

+133
-130
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ SparseArrays = "1"
107107
SpecialFunctions = "0.7, 0.8, 0.9, 0.10, 1.0, 2"
108108
StaticArrays = "0.10, 0.11, 0.12, 1.0"
109109
SymbolicIndexingInterface = "0.3.12"
110-
SymbolicUtils = "<1.6"
111-
Symbolics = "5.26"
110+
SymbolicUtils = "2"
111+
Symbolics = "5.30.1"
112112
URIs = "1"
113113
UnPack = "0.1, 1.0"
114114
Unitful = "1.1"

src/ModelingToolkit.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ using PrecompileTools, Reexport
99
end
1010

1111
import SymbolicUtils
12-
import SymbolicUtils: istree, arguments, operation, similarterm, promote_symtype,
12+
import SymbolicUtils: iscall, arguments, operation, maketerm, promote_symtype,
1313
Symbolic, isadd, ismul, ispow, issym, FnType,
1414
@rule, Rewriters, substitute, metadata, BasicSymbolic,
1515
Sym, Term
@@ -59,7 +59,7 @@ using Symbolics: _parse_vars, value, @derivatives, get_variables,
5959
exprs_occur_in, solve_for, build_expr, unwrap, wrap,
6060
VariableSource, getname, variable, Connection, connect,
6161
NAMESPACE_SEPARATOR, set_scalar_metadata, setdefaultval,
62-
initial_state, transition, activeState, entry,
62+
initial_state, transition, activeState, entry, hasnode,
6363
ticksInState, timeInState, fixpoint_sub, fast_substitute
6464
import Symbolics: rename, get_variables!, _solve, hessian_sparsity,
6565
jacobian_sparsity, isaffine, islinear, _iszero, _isone,

src/clock.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function is_continuous_domain(x)
2121
end
2222

2323
function get_time_domain(x)
24-
if istree(x) && operation(x) isa Operator
24+
if iscall(x) && operation(x) isa Operator
2525
output_timedomain(x)
2626
else
2727
getmetadata(x, TimeDomain, nothing)
@@ -130,7 +130,7 @@ is_concrete_time_domain(x) = x isa Union{AbstractClock, Continuous}
130130
SolverStepClock()
131131
SolverStepClock(t)
132132
133-
A clock that ticks at each solver step (sometimes referred to as "continuous sample time"). This clock **does generally not have equidistant tick intervals**, instead, the tick interval depends on the adaptive step-size slection of the continuous solver, as well as any continuous event handling. If adaptivity of the solver is turned off and there are no continuous events, the tick interval will be given by the fixed solver time step `dt`.
133+
A clock that ticks at each solver step (sometimes referred to as "continuous sample time"). This clock **does generally not have equidistant tick intervals**, instead, the tick interval depends on the adaptive step-size slection of the continuous solver, as well as any continuous event handling. If adaptivity of the solver is turned off and there are no continuous events, the tick interval will be given by the fixed solver time step `dt`.
134134
135135
Due to possibly non-equidistant tick intervals, this clock should typically not be used with discrete-time systems that assume a fixed sample time, such as PID controllers and digital filters.
136136
"""

src/debugging.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ end
2828

2929
debug_sub(eq::Equation) = debug_sub(eq.lhs) ~ debug_sub(eq.rhs)
3030
function debug_sub(ex)
31-
istree(ex) || return ex
31+
iscall(ex) || return ex
3232
f = operation(ex)
3333
args = map(debug_sub, arguments(ex))
3434
f in LOGGED_FUN ? logged_fun(f, args...) :
35-
similarterm(ex, f, args, metadata = metadata(ex))
35+
maketerm(typeof(ex), f, args, symtype(t), metadata(ex))
3636
end

src/discretedomain.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ end
3939
function (D::Shift)(x::Num, allow_zero = false)
4040
!allow_zero && D.steps == 0 && return x
4141
vt = value(x)
42-
if istree(vt)
42+
if iscall(vt)
4343
op = operation(vt)
4444
if op isa Sample
4545
error("Cannot shift a `Sample`. Create a variable to represent the sampled value and shift that instead")

src/inputoutput.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ function outputs(sys)
1919
lhss = [eq.lhs for eq in o]
2020
unique([filter(isoutput, unknowns(sys))
2121
filter(isoutput, parameters(sys))
22-
filter(x -> istree(x) && isoutput(x), rhss) # observed can return equations with complicated expressions, we are only looking for single Terms
23-
filter(x -> istree(x) && isoutput(x), lhss)])
22+
filter(x -> iscall(x) && isoutput(x), rhss) # observed can return equations with complicated expressions, we are only looking for single Terms
23+
filter(x -> iscall(x) && isoutput(x), lhss)])
2424
end
2525

2626
"""

src/parameters.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ function isparameter(x)
1515
isparameter(p) ||
1616
(hasmetadata(p, Symbolics.VariableSource) &&
1717
getmetadata(p, Symbolics.VariableSource)[1] == :parameters)
18-
elseif istree(x) && operation(x) isa Symbolic
18+
elseif iscall(x) && operation(x) isa Symbolic
1919
varT === PARAMETER || isparameter(operation(x))
20-
elseif istree(x) && operation(x) == (getindex)
20+
elseif iscall(x) && operation(x) == (getindex)
2121
isparameter(arguments(x)[1])
2222
elseif x isa Symbolic
2323
varT === PARAMETER

src/structural_transformation/StructuralTransformations.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ using Symbolics: unwrap, linear_expansion, fast_substitute
77
using SymbolicUtils
88
using SymbolicUtils.Code
99
using SymbolicUtils.Rewriters
10-
using SymbolicUtils: similarterm, istree
10+
using SymbolicUtils: maketerm, iscall
1111

1212
using ModelingToolkit
1313
using ModelingToolkit: ODESystem, AbstractSystem, var_from_nested_derivative, Differential,

src/structural_transformation/symbolics_tearing.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function full_equations(sys::AbstractSystem; simplify = false)
9292
@unpack subs = substitutions
9393
solved = Dict(eq.lhs => eq.rhs for eq in subs)
9494
neweqs = map(equations(sys)) do eq
95-
if istree(eq.lhs) && operation(eq.lhs) isa Union{Shift, Differential}
95+
if iscall(eq.lhs) && operation(eq.lhs) isa Union{Shift, Differential}
9696
return tearing_sub(eq.lhs, solved, simplify) ~ tearing_sub(eq.rhs, solved,
9797
simplify)
9898
else
@@ -568,7 +568,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching,
568568

569569
for eq in obs
570570
lhs = eq.lhs
571-
istree(lhs) || continue
571+
iscall(lhs) || continue
572572
operation(lhs) === getindex || continue
573573
Symbolics.shape(lhs) !== Symbolics.Unknown() || continue
574574
arg1 = arguments(lhs)[1]

src/structural_transformation/utils.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ function simplify_shifts(var)
445445
t2 = op2.t
446446
return simplify_shifts(ModelingToolkit.Shift(t1 === nothing ? t2 : t1, s1 + s2)(vv2))
447447
else
448-
return similarterm(var, operation(var), simplify_shifts.(arguments(var)),
449-
Symbolics.symtype(var); metadata = unwrap(var).metadata)
448+
return maketerm(typeof(var), operation(var), simplify_shifts.(arguments(var)),
449+
Symbolics.symtype(var), unwrap(var).metadata)
450450
end
451451
end

src/systems/abstractsystem.jl

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ function wrap_array_vars(sys::AbstractSystem, exprs; dvs = unknowns(sys))
205205
isscalar = !(exprs isa AbstractArray)
206206
array_vars = Dict{Any, AbstractArray{Int}}()
207207
for (j, x) in enumerate(dvs)
208-
if istree(x) && operation(x) == getindex
208+
if iscall(x) && operation(x) == getindex
209209
arg = arguments(x)[1]
210210
inds = get!(() -> Int[], array_vars, arg)
211211
push!(inds, j)
@@ -352,7 +352,7 @@ function SymbolicIndexingInterface.is_variable(sys::AbstractSystem, sym)
352352
end
353353
if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing
354354
return is_variable(ic, sym) ||
355-
istree(sym) && operation(sym) === getindex &&
355+
iscall(sym) && operation(sym) === getindex &&
356356
is_variable(ic, first(arguments(sym)))
357357
end
358358
return any(isequal(sym), variable_symbols(sys)) ||
@@ -378,7 +378,7 @@ function SymbolicIndexingInterface.variable_index(sys::AbstractSystem, sym)
378378
if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing
379379
return if (idx = variable_index(ic, sym)) !== nothing
380380
idx
381-
elseif istree(sym) && operation(sym) === getindex &&
381+
elseif iscall(sym) && operation(sym) === getindex &&
382382
(idx = variable_index(ic, first(arguments(sym)))) !== nothing
383383
idx[arguments(sym)[(begin + 1):end]...]
384384
else
@@ -416,7 +416,7 @@ function SymbolicIndexingInterface.is_parameter(sys::AbstractSystem, sym)
416416
sym = unwrap(sym)
417417
if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing
418418
return is_parameter(ic, sym) ||
419-
istree(sym) && operation(sym) === getindex &&
419+
iscall(sym) && operation(sym) === getindex &&
420420
is_parameter(ic, first(arguments(sym)))
421421
end
422422
if unwrap(sym) isa Int
@@ -441,7 +441,7 @@ function SymbolicIndexingInterface.parameter_index(sys::AbstractSystem, sym)
441441
if has_index_cache(sys) && (ic = get_index_cache(sys)) !== nothing
442442
return if (idx = parameter_index(ic, sym)) !== nothing
443443
idx
444-
elseif istree(sym) && operation(sym) === getindex &&
444+
elseif iscall(sym) && operation(sym) === getindex &&
445445
(idx = parameter_index(ic, first(arguments(sym)))) !== nothing
446446
ParameterIndex(idx.portion, (idx.idx..., arguments(sym)[(begin + 1):end]...))
447447
else
@@ -745,22 +745,22 @@ function _apply_to_variables(f::F, ex) where {F}
745745
if isvariable(ex)
746746
return f(ex)
747747
end
748-
istree(ex) || return ex
749-
similarterm(ex, _apply_to_variables(f, operation(ex)),
748+
iscall(ex) || return ex
749+
maketerm(typeof(ex), _apply_to_variables(f, operation(ex)),
750750
map(Base.Fix1(_apply_to_variables, f), arguments(ex)),
751-
metadata = metadata(ex))
751+
symtype(ex), metadata(ex))
752752
end
753753

754754
abstract type SymScope end
755755

756756
struct LocalScope <: SymScope end
757757
function LocalScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num}})
758758
apply_to_variables(sym) do sym
759-
if istree(sym) && operation(sym) === getindex
759+
if iscall(sym) && operation(sym) === getindex
760760
args = arguments(sym)
761761
a1 = setmetadata(args[1], SymScope, LocalScope())
762-
similarterm(sym, operation(sym), [a1, args[2:end]...];
763-
metadata = metadata(sym))
762+
maketerm(typeof(sym), operation(sym), [a1, args[2:end]...],
763+
symtype(sym), metadata(sym))
764764
else
765765
setmetadata(sym, SymScope, LocalScope())
766766
end
@@ -772,12 +772,12 @@ struct ParentScope <: SymScope
772772
end
773773
function ParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num}})
774774
apply_to_variables(sym) do sym
775-
if istree(sym) && operation(sym) === getindex
775+
if iscall(sym) && operation(sym) === getindex
776776
args = arguments(sym)
777777
a1 = setmetadata(args[1], SymScope,
778778
ParentScope(getmetadata(value(args[1]), SymScope, LocalScope())))
779-
similarterm(sym, operation(sym), [a1, args[2:end]...];
780-
metadata = metadata(sym))
779+
maketerm(typeof(sym), operation(sym), [a1, args[2:end]...],
780+
symtype(sym), metadata(sym))
781781
else
782782
setmetadata(sym, SymScope,
783783
ParentScope(getmetadata(value(sym), SymScope, LocalScope())))
@@ -791,12 +791,12 @@ struct DelayParentScope <: SymScope
791791
end
792792
function DelayParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num}}, N)
793793
apply_to_variables(sym) do sym
794-
if istree(sym) && operation(sym) == getindex
794+
if iscall(sym) && operation(sym) == getindex
795795
args = arguments(sym)
796796
a1 = setmetadata(args[1], SymScope,
797797
DelayParentScope(getmetadata(value(args[1]), SymScope, LocalScope()), N))
798-
similarterm(sym, operation(sym), [a1, args[2:end]...];
799-
metadata = metadata(sym))
798+
maketerm(typeof(sym), operation(sym), [a1, args[2:end]...],
799+
symtype(sym), metadata(sym))
800800
else
801801
setmetadata(sym, SymScope,
802802
DelayParentScope(getmetadata(value(sym), SymScope, LocalScope()), N))
@@ -808,11 +808,11 @@ DelayParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num}}) = DelayParentSco
808808
struct GlobalScope <: SymScope end
809809
function GlobalScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num}})
810810
apply_to_variables(sym) do sym
811-
if istree(sym) && operation(sym) == getindex
811+
if iscall(sym) && operation(sym) == getindex
812812
args = arguments(sym)
813813
a1 = setmetadata(args[1], SymScope, GlobalScope())
814-
similarterm(sym, operation(sym), [a1, args[2:end]...];
815-
metadata = metadata(sym))
814+
maketerm(typeof(sym), operation(sym), [a1, args[2:end]...],
815+
symtype(sym), metadata(sym))
816816
else
817817
setmetadata(sym, SymScope, GlobalScope())
818818
end
@@ -827,16 +827,16 @@ function renamespace(sys, x)
827827
x = unwrap(x)
828828
if x isa Symbolic
829829
T = typeof(x)
830-
if istree(x) && operation(x) isa Operator
831-
return similarterm(x, operation(x),
832-
Any[renamespace(sys, only(arguments(x)))];
833-
metadata = metadata(x))::T
830+
if iscall(x) && operation(x) isa Operator
831+
return maketerm(typeof(x), operation(x),
832+
Any[renamespace(sys, only(arguments(x)))],
833+
symtype(x), metadata(x))::T
834834
end
835-
if istree(x) && operation(x) === getindex
835+
if iscall(x) && operation(x) === getindex
836836
args = arguments(x)
837-
return similarterm(
838-
x, operation(x), vcat(renamespace(sys, args[1]), args[2:end]);
839-
metadata = metadata(x))::T
837+
return maketerm(
838+
typeof(x), operation(x), vcat(renamespace(sys, args[1]), args[2:end]),
839+
symtype(x), metadata(x))::T
840840
end
841841
let scope = getmetadata(x, SymScope, LocalScope())
842842
if scope isa LocalScope
@@ -909,7 +909,7 @@ function namespace_expr(
909909
O = unwrap(O)
910910
if any(isequal(O), ivs)
911911
return O
912-
elseif istree(O)
912+
elseif iscall(O)
913913
T = typeof(O)
914914
renamed = let sys = sys, n = n, T = T
915915
map(a -> namespace_expr(a, sys, n; ivs)::Any, arguments(O))
@@ -918,13 +918,14 @@ function namespace_expr(
918918
# Use renamespace so the scope is correct, and make sure to use the
919919
# metadata from the rescoped variable
920920
rescoped = renamespace(n, O)
921-
similarterm(O, operation(rescoped), renamed,
922-
metadata = metadata(rescoped))
921+
maketerm(typeof(rescoped), operation(rescoped), renamed,
922+
symtype(rescoped),
923+
metadata(rescoped))
923924
elseif Symbolics.isarraysymbolic(O)
924925
# promote_symtype doesn't work for array symbolics
925-
similarterm(O, operation(O), renamed, symtype(O), metadata = metadata(O))
926+
maketerm(typeof(O), operation(O), renamed, symtype(O), metadata(O))
926927
else
927-
similarterm(O, operation(O), renamed, metadata = metadata(O))
928+
maketerm(typeof(O), operation(O), renamed, symtype(O), metadata(O))
928929
end
929930
elseif isvariable(O)
930931
renamespace(n, O)
@@ -1116,7 +1117,7 @@ function time_varying_as_func(x, sys::AbstractTimeDependentSystem)
11161117
# than pass in a value in place of x(t).
11171118
#
11181119
# This is done by just making `x` the argument of the function.
1119-
if istree(x) &&
1120+
if iscall(x) &&
11201121
issym(operation(x)) &&
11211122
!(length(arguments(x)) == 1 && isequal(arguments(x)[1], get_iv(sys)))
11221123
return operation(x)
@@ -1144,7 +1145,7 @@ function push_vars!(stmt, name, typ, vars)
11441145
isempty(vars) && return
11451146
vars_expr = Expr(:macrocall, typ, nothing)
11461147
for s in vars
1147-
if istree(s)
1148+
if iscall(s)
11481149
f = nameof(operation(s))
11491150
args = arguments(s)
11501151
ex = :($f($(args...)))
@@ -1161,7 +1162,7 @@ function round_trip_expr(t, var2name)
11611162
name = get(var2name, t, nothing)
11621163
name !== nothing && return name
11631164
issym(t) && return nameof(t)
1164-
istree(t) || return t
1165+
iscall(t) || return t
11651166
f = round_trip_expr(operation(t), var2name)
11661167
args = map(Base.Fix2(round_trip_expr, var2name), arguments(t))
11671168
return :($f($(args...)))
@@ -2583,8 +2584,10 @@ is_diff_equation(eq2) # false
25832584
"""
25842585
function is_diff_equation(eq)
25852586
(eq isa Equation) || (return false)
2586-
isdefined(eq, :lhs) && occursin(is_derivative, wrap(eq.lhs)) && (return true)
2587-
isdefined(eq, :rhs) && occursin(is_derivative, wrap(eq.rhs)) && (return true)
2587+
isdefined(eq, :lhs) && hasnode(is_derivative, wrap(eq.lhs)) &&
2588+
(return true)
2589+
isdefined(eq, :rhs) && hasnode(is_derivative, wrap(eq.rhs)) &&
2590+
(return true)
25882591
return false
25892592
end
25902593

src/systems/clock_inference.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function substitute_sample_time(eq::Equation, dt)
4848
end
4949

5050
function substitute_sample_time(ex, dt)
51-
istree(ex) || return ex
51+
iscall(ex) || return ex
5252
op = operation(ex)
5353
args = arguments(ex)
5454
if op == SampleTime
@@ -63,7 +63,7 @@ function substitute_sample_time(ex, dt)
6363
end
6464
new_args[i] = ex_arg
6565
end
66-
similarterm(ex, op, new_args, symtype(ex); metadata = metadata(ex))
66+
maketerm(typeof(ex), op, new_args, symtype(ex), metadata(ex))
6767
end
6868
end
6969

@@ -128,7 +128,7 @@ function resize_or_push!(v, val, idx)
128128
end
129129

130130
function is_time_domain_conversion(v)
131-
istree(v) && (o = operation(v)) isa Operator &&
131+
iscall(v) && (o = operation(v)) isa Operator &&
132132
input_timedomain(o) != output_timedomain(o)
133133
end
134134

src/systems/connectors.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ end
1414

1515
function get_connection_type(s)
1616
s = unwrap(s)
17-
if istree(s) && operation(s) === getindex
17+
if iscall(s) && operation(s) === getindex
1818
s = arguments(s)[1]
1919
end
2020
getmetadata(s, VariableConnectType, Equality)
@@ -82,7 +82,7 @@ function collect_instream!(set, eq::Equation)
8282
end
8383

8484
function collect_instream!(set, expr, occurs = false)
85-
istree(expr) || return occurs
85+
iscall(expr) || return occurs
8686
op = operation(expr)
8787
op === instream && (push!(set, expr); occurs = true)
8888
for a in SymbolicUtils.unsorted_arguments(expr)

0 commit comments

Comments
 (0)