Skip to content

Commit d355ebb

Browse files
refactor: make symbol_to_symbolic a global utility function
1 parent 8adc7af commit d355ebb

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/systems/diffeqs/odesystem.jl

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -445,20 +445,10 @@ function build_explicit_observed_function(sys, ts;
445445
end
446446

447447
allsyms = all_symbols(sys)
448-
function symbol_to_symbolic(sym)
449-
sym isa Symbol || return sym
450-
idx = findfirst(x -> (hasname(x) ? getname(x) : Symbol(x)) == sym, allsyms)
451-
idx === nothing && return sym
452-
sym = allsyms[idx]
453-
if iscall(sym) && operation(sym) == getindex
454-
sym = arguments(sym)[1]
455-
end
456-
return sym
457-
end
458448
if symbolic_type(ts) == NotSymbolic() && ts isa AbstractArray
459-
ts = map(symbol_to_symbolic, ts)
449+
ts = map(x -> symbol_to_symbolic(sys, x; allsyms), ts)
460450
else
461-
ts = symbol_to_symbolic(ts)
451+
ts = symbol_to_symbolic(sys, ts; allsyms)
462452
end
463453

464454
vs = ModelingToolkit.vars(ts; op)

src/utils.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,3 +1249,21 @@ function process_equations(eqs, iv)
12491249

12501250
diffvars, allunknowns, ps, Equation[diffeq; algeeq; compressed_eqs]
12511251
end
1252+
1253+
"""
1254+
$(TYPEDSIGNATURES)
1255+
1256+
If `sym isa Symbol`, try and convert it to a symbolic by matching against symbolic
1257+
variables in `allsyms`. If `sym` is not a `Symbol` or no match was found, return
1258+
`sym` as-is.
1259+
"""
1260+
function symbol_to_symbolic(sys::AbstractSystem, sym; allsyms = all_symbols(sys))
1261+
sym isa Symbol || return sym
1262+
idx = findfirst(x -> (hasname(x) ? getname(x) : Symbol(x)) == sym, allsyms)
1263+
idx === nothing && return sym
1264+
sym = allsyms[idx]
1265+
if iscall(sym) && operation(sym) == getindex
1266+
sym = arguments(sym)[1]
1267+
end
1268+
return sym
1269+
end

0 commit comments

Comments
 (0)