-
-
Notifications
You must be signed in to change notification settings - Fork 210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: implementation of new SymbolicIndexingInterface #2345
Merged
ChrisRackauckas
merged 1 commit into
SciML:master
from
AayushSabharwal:as/indexing-rework
Dec 12, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -161,10 +161,18 @@ function independent_variable(sys::AbstractSystem) | |
isdefined(sys, :iv) ? getfield(sys, :iv) : nothing | ||
end | ||
|
||
#Treat the result as a vector of symbols always | ||
function SymbolicIndexingInterface.independent_variables(sys::AbstractSystem) | ||
systype = typeof(sys) | ||
@warn "Please declare ($systype) as a subtype of `AbstractTimeDependentSystem`, `AbstractTimeIndependentSystem` or `AbstractMultivariateSystem`." | ||
function independent_variables(sys::AbstractTimeDependentSystem) | ||
return [getfield(sys, :iv)] | ||
end | ||
|
||
independent_variables(::AbstractTimeIndependentSystem) = [] | ||
|
||
function independent_variables(sys::AbstractMultivariateSystem) | ||
return getfield(sys, :ivs) | ||
end | ||
|
||
function independent_variables(sys::AbstractSystem) | ||
@warn "Please declare ($(typeof(sys))) as a subtype of `AbstractTimeDependentSystem`, `AbstractTimeIndependentSystem` or `AbstractMultivariateSystem`." | ||
if isdefined(sys, :iv) | ||
return [getfield(sys, :iv)] | ||
elseif isdefined(sys, :ivs) | ||
|
@@ -174,14 +182,102 @@ function SymbolicIndexingInterface.independent_variables(sys::AbstractSystem) | |
end | ||
end | ||
|
||
function SymbolicIndexingInterface.independent_variables(sys::AbstractTimeDependentSystem) | ||
[getfield(sys, :iv)] | ||
#Treat the result as a vector of symbols always | ||
function SymbolicIndexingInterface.is_variable(sys::AbstractSystem, sym) | ||
if unwrap(sym) isa Int # [x, 1] coerces 1 to a Num | ||
return unwrap(sym) in 1:length(unknown_states(sys)) | ||
end | ||
return any(isequal(sym), unknown_states(sys)) || hasname(sym) && is_variable(sys, getname(sym)) | ||
end | ||
|
||
function SymbolicIndexingInterface.is_variable(sys::AbstractSystem, sym::Symbol) | ||
return any(isequal(sym), getname.(unknown_states(sys))) || count('₊', string(sym)) == 1 && count(isequal(sym), Symbol.(sys.name, :₊, getname.(unknown_states(sys)))) == 1 | ||
end | ||
SymbolicIndexingInterface.independent_variables(sys::AbstractTimeIndependentSystem) = [] | ||
function SymbolicIndexingInterface.independent_variables(sys::AbstractMultivariateSystem) | ||
getfield(sys, :ivs) | ||
|
||
function SymbolicIndexingInterface.variable_index(sys::AbstractSystem, sym) | ||
if unwrap(sym) isa Int | ||
return unwrap(sym) | ||
end | ||
idx = findfirst(isequal(sym), unknown_states(sys)) | ||
if idx === nothing && hasname(sym) | ||
idx = variable_index(sys, getname(sym)) | ||
end | ||
return idx | ||
end | ||
|
||
function SymbolicIndexingInterface.variable_index(sys::AbstractSystem, sym::Symbol) | ||
idx = findfirst(isequal(sym), getname.(unknown_states(sys))) | ||
if idx !== nothing | ||
return idx | ||
elseif count('₊', string(sym)) == 1 | ||
return findfirst(isequal(sym), Symbol.(sys.name, :₊, getname.(unknown_states(sys)))) | ||
end | ||
return nothing | ||
end | ||
|
||
function SymbolicIndexingInterface.variable_symbols(sys::AbstractSystem) | ||
return unknown_states(sys) | ||
end | ||
|
||
function SymbolicIndexingInterface.is_parameter(sys::AbstractSystem, sym) | ||
if unwrap(sym) isa Int | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return unwrap(sym) in 1:length(parameters(sys)) | ||
end | ||
|
||
return any(isequal(sym), parameters(sys)) || hasname(sym) && is_parameter(sys, getname(sym)) | ||
end | ||
|
||
function SymbolicIndexingInterface.is_parameter(sys::AbstractSystem, sym::Symbol) | ||
return any(isequal(sym), getname.(parameters(sys))) || | ||
count('₊', string(sym)) == 1 && count(isequal(sym), Symbol.(sys.name, :₊, getname.(parameters(sys)))) == 1 | ||
end | ||
|
||
function SymbolicIndexingInterface.parameter_index(sys::AbstractSystem, sym) | ||
if unwrap(sym) isa Int | ||
return unwrap(sym) | ||
end | ||
idx = findfirst(isequal(sym), parameters(sys)) | ||
if idx === nothing && hasname(sym) | ||
idx = parameter_index(sys, getname(sym)) | ||
end | ||
return idx | ||
end | ||
|
||
function SymbolicIndexingInterface.parameter_index(sys::AbstractSystem, sym::Symbol) | ||
idx = findfirst(isequal(sym), getname.(parameters(sys))) | ||
if idx !== nothing | ||
return idx | ||
elseif count('₊', string(sym)) == 1 | ||
return findfirst(isequal(sym), Symbol.(sys.name, :₊, getname.(parameters(sys)))) | ||
end | ||
return nothing | ||
end | ||
|
||
function SymbolicIndexingInterface.parameter_symbols(sys::AbstractSystem) | ||
return parameters(sys) | ||
end | ||
|
||
function SymbolicIndexingInterface.is_independent_variable(sys::AbstractSystem, sym) | ||
return any(isequal(sym), independent_variables(sys)) | ||
end | ||
|
||
function SymbolicIndexingInterface.is_independent_variable(sys::AbstractSystem, sym::Symbol) | ||
return any(isequal(sym), getname.(independent_variables(sys))) | ||
end | ||
|
||
function SymbolicIndexingInterface.independent_variable_symbols(sys::AbstractSystem) | ||
return independent_variables(sys) | ||
end | ||
|
||
function SymbolicIndexingInterface.is_observed(sys::AbstractSystem, sym) | ||
return !is_variable(sys, sym) && !is_parameter(sys, sym) && !is_independent_variable(sys, sym) && symbolic_type(sym) != NotSymbolic() | ||
end | ||
|
||
SymbolicIndexingInterface.is_time_dependent(::AbstractTimeDependentSystem) = true | ||
SymbolicIndexingInterface.is_time_dependent(::AbstractTimeIndependentSystem) = false | ||
|
||
SymbolicIndexingInterface.constant_structure(::AbstractSystem) = true | ||
|
||
iscomplete(sys::AbstractSystem) = isdefined(sys, :complete) && getfield(sys, :complete) | ||
|
||
""" | ||
|
@@ -534,12 +630,15 @@ function states(sys::AbstractSystem) | |
[sts; reduce(vcat, namespace_variables.(systems))]) | ||
end | ||
|
||
function SymbolicIndexingInterface.parameters(sys::AbstractSystem) | ||
function parameters(sys::AbstractSystem) | ||
ps = get_ps(sys) | ||
systems = get_systems(sys) | ||
unique(isempty(systems) ? ps : [ps; reduce(vcat, namespace_parameters.(systems))]) | ||
end | ||
|
||
# required in `src/connectors.jl:437` | ||
parameters(_) = [] | ||
|
||
function controls(sys::AbstractSystem) | ||
ctrls = get_ctrls(sys) | ||
systems = get_systems(sys) | ||
|
@@ -638,8 +737,6 @@ function time_varying_as_func(x, sys::AbstractTimeDependentSystem) | |
return x | ||
end | ||
|
||
SymbolicIndexingInterface.is_indep_sym(sys::AbstractSystem, sym) = isequal(sym, get_iv(sys)) | ||
|
||
""" | ||
$(SIGNATURES) | ||
|
||
|
@@ -653,20 +750,6 @@ function unknown_states(sys::AbstractSystem) | |
return sts | ||
end | ||
|
||
function SymbolicIndexingInterface.state_sym_to_index(sys::AbstractSystem, sym) | ||
findfirst(isequal(sym), unknown_states(sys)) | ||
end | ||
function SymbolicIndexingInterface.is_state_sym(sys::AbstractSystem, sym) | ||
!isnothing(SymbolicIndexingInterface.state_sym_to_index(sys, sym)) | ||
end | ||
|
||
function SymbolicIndexingInterface.param_sym_to_index(sys::AbstractSystem, sym) | ||
findfirst(isequal(sym), SymbolicIndexingInterface.parameters(sys)) | ||
end | ||
function SymbolicIndexingInterface.is_param_sym(sys::AbstractSystem, sym) | ||
!isnothing(SymbolicIndexingInterface.param_sym_to_index(sys, sym)) | ||
end | ||
|
||
### | ||
### System utils | ||
### | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems really ugly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it is. It's the only way I could find to cover all the cases
isa Int
check because[x, 1]
wraps1
in aNum
(e.g.sol(1.0, idxs=[1, x])
sol[x + y]
is a possibility, andgetname
errors onx+y
getname
case is necessary for namespaced variablesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what does
unwrap
do?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It gets the value inside a
Num
, which is usually either aSymbolicUtils.BasicSymbolic
for symbolic variables or a number in the case of something likeNum(1)