@@ -467,23 +467,56 @@ function find_derivatives!(vars, expr, f)
467467 return vars
468468end
469469
470- function collect_vars! (unknowns, parameters, expr, iv; op = Differential)
470+ """
471+ $(TYPEDSIGNATURES)
472+
473+ Search through equations and parameter dependencies of `sys`, where sys is at a depth of
474+ `depth` from the root system, looking for variables scoped to the root system. Also
475+ recursively searches through all subsystems of `sys`, increasing the depth if it is not
476+ `-1`. A depth of `-1` indicates searching for variables with `GlobalScope`.
477+ """
478+ function collect_scoped_vars! (unknowns, parameters, sys, iv; depth = 1 , op = Differential)
479+ if has_eqs (sys)
480+ for eq in get_eqs (sys)
481+ eq. lhs isa Union{Symbolic, Number} || continue
482+ collect_vars! (unknowns, parameters, eq. lhs, iv; depth, op)
483+ collect_vars! (unknowns, parameters, eq. rhs, iv; depth, op)
484+ end
485+ end
486+ if has_parameter_dependencies (sys)
487+ for eq in get_parameter_dependencies (sys)
488+ if eq isa Pair
489+ collect_vars! (unknowns, parameters, eq[1 ], iv; depth, op)
490+ collect_vars! (unknowns, parameters, eq[2 ], iv; depth, op)
491+ else
492+ collect_vars! (unknowns, parameters, eq. lhs, iv; depth, op)
493+ collect_vars! (unknowns, parameters, eq. rhs, iv; depth, op)
494+ end
495+ end
496+ end
497+ newdepth = depth == - 1 ? depth : depth + 1
498+ for ssys in get_systems (sys)
499+ collect_scoped_vars! (unknowns, parameters, ssys, iv; depth = newdepth, op)
500+ end
501+ end
502+
503+ function collect_vars! (unknowns, parameters, expr, iv; depth = 0 , op = Differential)
471504 if issym (expr)
472- collect_var! (unknowns, parameters, expr, iv)
505+ collect_var! (unknowns, parameters, expr, iv; depth )
473506 else
474507 for var in vars (expr; op)
475508 if iscall (var) && operation (var) isa Differential
476509 var, _ = var_from_nested_derivative (var)
477510 end
478- collect_var! (unknowns, parameters, var, iv)
511+ collect_var! (unknowns, parameters, var, iv; depth )
479512 end
480513 end
481514 return nothing
482515end
483516
484- function collect_var! (unknowns, parameters, var, iv)
517+ function collect_var! (unknowns, parameters, var, iv; depth = 0 )
485518 isequal (var, iv) && return nothing
486- getmetadata (var, SymScope, LocalScope ()) == LocalScope ( ) || return nothing
519+ check_scope_depth ( getmetadata (var, SymScope, LocalScope ()), depth ) || return nothing
487520 if iscalledparameter (var)
488521 callable = getcalledparameter (var)
489522 push! (parameters, callable)
@@ -500,6 +533,24 @@ function collect_var!(unknowns, parameters, var, iv)
500533 return nothing
501534end
502535
536+ """
537+ $(TYPEDSIGNATURES)
538+
539+ Check if the given `scope` is at a depth of `depth` from the root system. Only
540+ returns `true` for `scope::GlobalScope` if `depth == -1`.
541+ """
542+ function check_scope_depth (scope, depth)
543+ if scope isa LocalScope
544+ return depth == 0
545+ elseif scope isa ParentScope
546+ return depth > 0 && check_scope_depth (scope. parent, depth - 1 )
547+ elseif scope isa DelayParentScope
548+ return depth >= scope. N && check_scope_depth (scope. parent, depth - scope. N)
549+ elseif scope isa GlobalScope
550+ return depth == - 1
551+ end
552+ end
553+
503554"""
504555Find all the symbolic constants of some equations or terms and return them as a vector.
505556"""
0 commit comments