Skip to content

Commit f1f2258

Browse files
Merge pull request #2926 from SciML/lin3
add an example to IO docs
2 parents 9949180 + 01c03cd commit f1f2258

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

docs/src/basics/InputOutput.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,60 @@ This documentation page lists utilities that are useful for working with inputs
2424

2525
## Generating a dynamics function with inputs, ``f``
2626

27-
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.
27+
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.
2828

29-
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``.
29+
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``.
3030

3131
!!! note "Un-simplified system"
3232

3333
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.
3434

35+
### Example:
36+
37+
38+
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.
39+
40+
41+
```@example inputoutput
42+
import ModelingToolkit: t_nounits as t, D_nounits as D
43+
@variables x(t)=0 u(t)=0 y(t)
44+
@parameters k = 1
45+
eqs = [D(x) ~ -k * (x + u)
46+
y ~ x]
47+
48+
@named sys = ODESystem(eqs, t)
49+
f, x_sym, ps = ModelingToolkit.generate_control_function(sys, [u], simplify = true);
50+
nothing # hide
51+
```
52+
53+
54+
We can inspect the state realization chosen by MTK
55+
56+
57+
```@example inputoutput
58+
x_sym
59+
```
60+
61+
as expected, `x` is chosen as the state variable.
62+
as expected, `x` is chosen as the state variable.
63+
64+
```@example inputoutput
65+
using Test # hide
66+
@test isequal(x_sym[], x) # hide
67+
@test isequal(ps, [k]) # hide
68+
nothing # hide
69+
```
70+
71+
Now we can test the generated function `f` with random input and state values
72+
73+
74+
```@example inputoutput
75+
p = [1]
76+
x = [rand()]
77+
u = [rand()]
78+
@test f[1](x, u, p, 1) ≈ -p[] * (x + u) # Test that the function computes what we expect D(x) = -k*(x + u)
79+
```
80+
3581
## Generating an output function, ``g``
3682

3783
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.

src/inputoutput.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ has_var(ex, x) = x ∈ Set(get_variables(ex))
160160
# Build control function
161161

162162
"""
163-
(f_oop, f_ip), dvs, p, io_sys = generate_control_function(
163+
(f_oop, f_ip), x_sym, p, io_sys = generate_control_function(
164164
sys::AbstractODESystem,
165165
inputs = unbound_inputs(sys),
166166
disturbance_inputs = nothing;
@@ -175,7 +175,7 @@ f_oop : (x,u,p,t) -> rhs
175175
f_ip : (xout,x,u,p,t) -> nothing
176176
```
177177
178-
The return values also include the remaining unknowns and parameters, in the order they appear as arguments to `f`.
178+
The return values also include the chosen state-realization (the remaining unknowns) `x_sym` and parameters, in the order they appear as arguments to `f`.
179179
180180
If `disturbance_inputs` is an array of variables, the generated dynamics function will preserve any state and dynamics associated with disturbance inputs, but the disturbance inputs themselves will not be included as inputs to the generated function. The use case for this is to generate dynamics for state observers that estimate the influence of unmeasured disturbances, and thus require unknown variables for the disturbance model, but without disturbance inputs since the disturbances are not available for measurement.
181181
See [`add_input_disturbance`](@ref) for a higher-level interface to this functionality.
@@ -187,9 +187,9 @@ See [`add_input_disturbance`](@ref) for a higher-level interface to this functio
187187
188188
```
189189
using ModelingToolkit: generate_control_function, varmap_to_vars, defaults
190-
f, dvs, ps = generate_control_function(sys, expression=Val{false}, simplify=false)
190+
f, x_sym, ps = generate_control_function(sys, expression=Val{false}, simplify=false)
191191
p = varmap_to_vars(defaults(sys), ps)
192-
x = varmap_to_vars(defaults(sys), dvs)
192+
x = varmap_to_vars(defaults(sys), x_sym)
193193
t = 0
194194
f[1](x, inputs, p, t)
195195
```

0 commit comments

Comments
 (0)