Skip to content

Commit 5a6b0fa

Browse files
committed
Minor optimization
1 parent 0681e75 commit 5a6b0fa

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

src/systems/abstractsystem.jl

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -389,25 +389,27 @@ function renamespace(sys, x)
389389
sys === nothing && return x
390390
x = unwrap(x)
391391
if x isa Symbolic
392+
T = typeof(x)
392393
if istree(x) && operation(x) isa Operator
393-
return similarterm(x, operation(x), Any[renamespace(sys, only(arguments(x)))])
394+
return similarterm(x, operation(x),
395+
Any[renamespace(sys, only(arguments(x)))])::T
394396
end
395397
let scope = getmetadata(x, SymScope, LocalScope())
396398
if scope isa LocalScope
397-
rename(x, renamespace(getname(sys), getname(x)))
399+
rename(x, renamespace(getname(sys), getname(x)))::T
398400
elseif scope isa ParentScope
399-
setmetadata(x, SymScope, scope.parent)
401+
setmetadata(x, SymScope, scope.parent)::T
400402
elseif scope isa DelayParentScope
401403
if scope.N > 0
402404
x = setmetadata(x, SymScope,
403405
DelayParentScope(scope.parent, scope.N - 1))
404-
rename(x, renamespace(getname(sys), getname(x)))
406+
rename(x, renamespace(getname(sys), getname(x)))::T
405407
else
406408
#rename(x, renamespace(getname(sys), getname(x)))
407-
setmetadata(x, SymScope, scope.parent)
409+
setmetadata(x, SymScope, scope.parent)::T
408410
end
409411
else # GlobalScope
410-
x
412+
x::T
411413
end
412414
end
413415
elseif x isa AbstractSystem
@@ -423,9 +425,8 @@ namespace_controls(sys::AbstractSystem) = controls(sys, controls(sys))
423425

424426
function namespace_defaults(sys)
425427
defs = defaults(sys)
426-
Dict((isparameter(k) ? parameters(sys, k) : states(sys, k)) => namespace_expr(defs[k],
427-
sys)
428-
for k in keys(defs))
428+
Dict((isparameter(k) ? parameters(sys, k) : states(sys, k)) => namespace_expr(v, sys)
429+
for (k, v) in pairs(defs))
429430
end
430431

431432
function namespace_equations(sys::AbstractSystem)
@@ -454,14 +455,19 @@ function namespace_expr(O, sys, n = nameof(sys))
454455
elseif isvariable(O)
455456
renamespace(n, O)
456457
elseif istree(O)
457-
renamed = map(a -> namespace_expr(a, sys, n), arguments(O))
458+
T = typeof(O)
458459
if symtype(operation(O)) <: FnType
459-
renamespace(n, O)
460+
renamespace(n, O)::T
460461
else
461-
similarterm(O, operation(O), renamed)
462+
renamed = let sys = sys, n = n, T = T
463+
map(a -> namespace_expr(a, sys, n)::Any, arguments(O))
464+
end
465+
similarterm(O, operation(O), renamed)::T
462466
end
463467
elseif O isa Array
464-
map(o -> namespace_expr(o, sys, n), O)
468+
let sys = sys, n = n
469+
map(o -> namespace_expr(o, sys, n), O)
470+
end
465471
else
466472
O
467473
end

src/systems/validation.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ get_unit(x::Real) = unitless
4040
get_unit(x::Unitful.Quantity) = screen_unit(Unitful.unit(x))
4141
get_unit(x::AbstractArray) = map(get_unit, x)
4242
get_unit(x::Num) = get_unit(value(x))
43-
get_unit(x::Union{Symbolics.ArrayOp, Symbolics.Arr, Symbolics.CallWithMetadata}) = get_literal_unit(x)
43+
function get_unit(x::Union{Symbolics.ArrayOp, Symbolics.Arr, Symbolics.CallWithMetadata})
44+
get_literal_unit(x)
45+
end
4446
get_unit(op::Differential, args) = get_unit(args[1]) / get_unit(op.x)
4547
get_unit(op::Difference, args) = get_unit(args[1]) / get_unit(op.t)
4648
get_unit(op::typeof(getindex), args) = get_unit(args[1])

src/utils.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,12 +343,13 @@ iv_from_nested_difference(x) = nothing
343343

344344
var_from_nested_difference(x, i = 0) = (nothing, nothing)
345345
function var_from_nested_difference(x::Symbolic, i = 0)
346-
istree(x) && operation(x) isa Difference ? var_from_nested_difference(arguments(x)[1], i + 1) :
346+
istree(x) && operation(x) isa Difference ?
347+
var_from_nested_difference(arguments(x)[1], i + 1) :
347348
(x, i)
348349
end
349350

350-
isvariable(x::Num) = isvariable(value(x))
351-
function isvariable(x)
351+
isvariable(x::Num)::Bool = isvariable(value(x))
352+
function isvariable(x)::Bool
352353
x isa Symbolic || return false
353354
p = getparent(x, nothing)
354355
p === nothing || (x = p)
@@ -369,7 +370,9 @@ v = ModelingToolkit.vars(D(y) ~ u)
369370
v == Set([D(y), u])
370371
```
371372
"""
372-
vars(exprs::Symbolic; op = Differential) = istree(exprs) ? vars([exprs]; op = op) : Set([exprs])
373+
function vars(exprs::Symbolic; op = Differential)
374+
istree(exprs) ? vars([exprs]; op = op) : Set([exprs])
375+
end
373376
vars(exprs; op = Differential) = foldl((x, y) -> vars!(x, y; op = op), exprs; init = Set())
374377
vars(eq::Equation; op = Differential) = vars!(Set(), eq; op = op)
375378
function vars!(vars, eq::Equation; op = Differential)

0 commit comments

Comments
 (0)