-
-
Notifications
You must be signed in to change notification settings - Fork 211
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
add an example to IO docs #2926
Changes from all commits
51afdc1
80b19c8
67d89da
07a7393
5d037e8
2cc5cad
01c03cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -24,14 +24,60 @@ This documentation page lists utilities that are useful for working with inputs | |||
|
||||
## Generating a dynamics function with inputs, ``f`` | ||||
|
||||
ModelingToolkit can generate the dynamics of a system, the function ``M\dot x = f(x, u, p, t)`` above, such that the user can pass not only the state ``x`` and parameters ``p`` but also an external input ``u``. To this end, the function [`generate_control_function`](@ref) exists. | ||||
ModelingToolkit can generate the dynamics of a system, the function ``M\dot x = f(x, u, p, t)`` above, such that the user can pass not only the state ``x`` and parameters ``p`` but also an external input ``u``. To this end, the function [`ModelingToolkit.generate_control_function`](@ref) exists. | ||||
|
||||
This function takes a vector of variables that are to be considered inputs, i.e., part of the vector ``u``. Alongside returning the function ``f``, [`generate_control_function`](@ref) also returns the chosen state realization of the system after simplification. This vector specifies the order of the state variables ``x``, while the user-specified vector `u` specifies the order of the input variables ``u``. | ||||
This function takes a vector of variables that are to be considered inputs, i.e., part of the vector ``u``. Alongside returning the function ``f``, [`ModelingToolkit.generate_control_function`](@ref) also returns the chosen state realization of the system after simplification. This vector specifies the order of the state variables ``x``, while the user-specified vector `u` specifies the order of the input variables ``u``. | ||||
|
||||
!!! note "Un-simplified system" | ||||
|
||||
This function expects `sys` to be un-simplified, i.e., `structural_simplify` or `@mtkbuild` should not be called on the system before passing it into this function. `generate_control_function` calls a special version of `structural_simplify` internally. | ||||
|
||||
### Example: | ||||
|
||||
|
||||
The following example implements a simple first-order system with an input `u` and state `x`. The function `f` is generated using `generate_control_function`, and the function `f` is then tested with random input and state values. | ||||
|
||||
|
||||
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. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||
```@example inputoutput | ||||
import ModelingToolkit: t_nounits as t, D_nounits as D | ||||
@variables x(t)=0 u(t)=0 y(t) | ||||
@parameters k = 1 | ||||
eqs = [D(x) ~ -k * (x + u) | ||||
y ~ x] | ||||
|
||||
@named sys = ODESystem(eqs, t) | ||||
f, x_sym, ps = ModelingToolkit.generate_control_function(sys, [u], simplify = true); | ||||
nothing # hide | ||||
``` | ||||
|
||||
|
||||
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. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||
We can inspect the state realization chosen by MTK | ||||
|
||||
|
||||
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. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||
```@example inputoutput | ||||
x_sym | ||||
``` | ||||
|
||||
as expected, `x` is chosen as the state variable. | ||||
as expected, `x` is chosen as the state variable. | ||||
|
||||
```@example inputoutput | ||||
using Test # hide | ||||
@test isequal(x_sym[], x) # hide | ||||
@test isequal(ps, [k]) # hide | ||||
nothing # hide | ||||
``` | ||||
|
||||
Now we can test the generated function `f` with random input and state values | ||||
|
||||
|
||||
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. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||
```@example inputoutput | ||||
p = [1] | ||||
x = [rand()] | ||||
u = [rand()] | ||||
@test f[1](x, u, p, 1) ≈ -p[] * (x + u) # Test that the function computes what we expect D(x) = -k*(x + u) | ||||
``` | ||||
|
||||
## Generating an output function, ``g`` | ||||
|
||||
ModelingToolkit can also generate a function that computes a specified output of a system, the function ``y = g(x, u, p, t)`` above. This is done using the function [`build_explicit_observed_function`](@ref). When generating an output function, the user must specify the output variable(s) of interest, as well as any inputs if inputs are relevant to compute the output. | ||||
|
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.
[JuliaFormatter] reported by reviewdog 🐶