Skip to content

Commit 51afdc1

Browse files
committed
add an example to IO docs
1 parent 50a4b12 commit 51afdc1

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

docs/src/basics/InputOutput.md

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

3777
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)