-
-
Notifications
You must be signed in to change notification settings - Fork 8
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: add all_solvable_symbols
and all_symbols
#18
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2107adc
feat: add `all_solvable_symbols` and `all_symbols`
AayushSabharwal 0b925c0
refactor: add new *_symbols methods, corresponding singletons, update…
AayushSabharwal aeb46b0
fixup! refactor: add new *_symbols methods, corresponding singletons,…
AayushSabharwal 8818979
fixup! refactor: add new *_symbols methods, corresponding singletons,…
AayushSabharwal 492fb01
fixup! refactor: add new *_symbols methods, corresponding singletons,…
AayushSabharwal ae50f9d
fixup! refactor: add new *_symbols methods, corresponding singletons,…
AayushSabharwal 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
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 |
---|---|---|
@@ -0,0 +1,147 @@ | ||
# Using the SymbolicIndexingInterface | ||
|
||
This tutorial will cover ways to use the interface for types that implement it. | ||
Consider the following example: | ||
|
||
```@example Usage | ||
using ModelingToolkit, OrdinaryDiffEq, SymbolicIndexingInterface, Plots | ||
@parameters σ ρ β | ||
@variables t x(t) y(t) z(t) w(t) | ||
D = Differential(t) | ||
eqs = [D(D(x)) ~ σ * (y - x), | ||
D(y) ~ x * (ρ - z) - y, | ||
D(z) ~ x * y - β * z, | ||
w ~ x + y + z] | ||
@named sys = ODESystem(eqs) | ||
sys = structural_simplify(sys) | ||
``` | ||
|
||
The system has 4 state variables, 3 parameters and one observed variable: | ||
```@example Usage | ||
observed(sys) | ||
``` | ||
|
||
Solving the system, | ||
```@example Usage | ||
u0 = [D(x) => 2.0, | ||
x => 1.0, | ||
y => 0.0, | ||
z => 0.0] | ||
p = [σ => 28.0, | ||
ρ => 10.0, | ||
β => 8 / 3] | ||
tspan = (0.0, 100.0) | ||
prob = ODEProblem(sys, u0, tspan, p, jac = true) | ||
sol = solve(prob, Tsit5()) | ||
``` | ||
|
||
We can obtain the timeseries of any time-dependent variable using `getindex` | ||
```@example Usage | ||
sol[x] | ||
``` | ||
|
||
This also works for arrays or tuples of variables, including observed quantities and | ||
independent variables, for interpolating solutions, and plotting: | ||
```@example Usage | ||
sol[[x, y]] | ||
``` | ||
|
||
```@example Usage | ||
sol[(t, w)] | ||
``` | ||
|
||
```@example Usage | ||
sol(1.3, idxs=x) | ||
``` | ||
|
||
```@example Usage | ||
sol(1.3, idxs=[x, w]) | ||
``` | ||
|
||
```@example Usage | ||
sol(1.3, idxs=[:y, :z]) | ||
``` | ||
|
||
```@example Usage | ||
plot(sol, idxs=x) | ||
``` | ||
|
||
If necessary, `Symbol`s can be used to refer to variables. This is only valid for | ||
symbolic variables for which [`hasname`](@ref) returns `true`. The `Symbol` used must | ||
match the one returned by [`getname`](@ref) for the variable. | ||
```@example Usage | ||
hasname(x) | ||
``` | ||
|
||
```@example Usage | ||
getname(x) | ||
``` | ||
|
||
```@example Usage | ||
sol[(:x, :w)] | ||
``` | ||
|
||
Note how when indexing with an array, the returned type is a `Vector{Array{Float64}}`, | ||
and when using a `Tuple`, the returned type is `Vector{Tuple{Float64, Float64}}`. | ||
To obtain the value of all state variables, we can use the shorthand: | ||
```@example Usage | ||
sol[solvedvariables] # equivalent to sol[variable_symbols(sol)] | ||
``` | ||
|
||
This does not include the observed variable `w`. To include observed variables in the | ||
output, the following shorthand is used: | ||
```@example Usage | ||
sol[allvariables] # equivalent to sol[all_variable_symbols(sol)] | ||
``` | ||
|
||
Parameters cannot be obtained using this syntax, and instead require using [`getp`](@ref) and [`setp`](@ref). | ||
|
||
```@example Usage | ||
σ_getter = getp(sys, σ) | ||
σ_getter(sol) # can also pass `prob` | ||
``` | ||
|
||
Note that this also supports arrays/tuples of parameter symbols: | ||
|
||
```@example Usage | ||
σ_ρ_getter = getp(sys, (σ, ρ)) | ||
σ_ρ_getter(sol) | ||
``` | ||
|
||
Now suppose the system has to be solved with a different value of the parameter `β`. | ||
|
||
```@example Usage | ||
β_setter = setp(sys, β) | ||
β_setter(prob, 3) | ||
``` | ||
|
||
The updated parameter values can be checked using [`parameter_values`](@ref). | ||
|
||
```@example Usage | ||
parameter_values(prob) | ||
``` | ||
|
||
Solving the new system, note that the parameter getter functions still work on the new | ||
solution object. | ||
|
||
```@example Usage | ||
sol2 = solve(prob, Tsit5()) | ||
σ_getter(sol) | ||
``` | ||
|
||
```@example Usage | ||
σ_ρ_getter(sol) | ||
``` | ||
|
||
To set the entire parameter vector at once, [`parameter_values`](@ref) can be used | ||
(note the usage of broadcasted assignment). | ||
|
||
```@example Usage | ||
parameter_values(prob) .= [29.0, 11.0, 2.5] | ||
parameter_values(prob) | ||
``` | ||
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. We should also mention symbolic interpolation indexing, i.e. 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. Fixed |
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
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
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.
BTW, is this using the optimized call now, i.e. a single call to the observed function?
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.
No, it's broadcasted over the array of symbols, so each one is computed individually. A single call to observed behaves differently:
For state variables (whose values are in
sol.u
) I'd imagine broadcasting is faster than theobserved
method? It avoids a generated function.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.
For state variables it's faster, then for observed it's faster to clump them up. Optimizing this a bit is probably a good idea in the future.