Skip to content

Commit 84643a2

Browse files
baggepinnenChrisRackauckasgithub-actions[bot]
authored
add docs for input-output handling (#2918)
* add docs for input-output handling addresses #2852 * Update docs/src/basics/InputOutput.md Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update docs/src/basics/InputOutput.md Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update docs/src/basics/Linearization.md Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update docs/src/basics/InputOutput.md Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update docs/src/basics/InputOutput.md Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update docs/src/basics/InputOutput.md Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update docs/src/basics/Linearization.md Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update docs/src/basics/InputOutput.md * Update docs/src/basics/InputOutput.md * Update docs/src/basics/InputOutput.md Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update docs/src/basics/Linearization.md Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update docs/src/basics/InputOutput.md Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update docs/src/basics/Linearization.md Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update docs/src/basics/InputOutput.md Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --------- Co-authored-by: Christopher Rackauckas <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent fbc2f40 commit 84643a2

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

docs/pages.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pages = [
2828
"basics/Composition.md",
2929
"basics/Events.md",
3030
"basics/Linearization.md",
31+
"basics/InputOutput.md",
3132
"basics/MTKLanguage.md",
3233
"basics/Validation.md",
3334
"basics/DependencyGraphs.md",

docs/src/basics/InputOutput.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# [Input output](@id inputoutput)
2+
3+
An input-output system is a system on the form
4+
5+
```math
6+
\begin{aligned}
7+
M \dot x &= f(x, u, p, t) \\
8+
y &= g(x, u, p, t)
9+
```
10+
11+
where ``x`` is the state, ``u`` is the input and ``y`` is an output (in some contexts called an _observed variables_ in MTK).
12+
13+
While many uses of ModelingToolkit for simulation do not require the user to think about inputs and outputs (IO), there are certain situations in which handling IO explicitly may be important, such as
14+
15+
- Linearization
16+
- Control design
17+
- System identification
18+
- FMU export
19+
- Real-time simulation with external data inputs
20+
- Custom interfacing with other simulation tools
21+
22+
This documentation page lists utilities that are useful for working with inputs and outputs in ModelingToolkit.
23+
24+
## Generating a dynamics function with inputs, ``f``
25+
26+
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+
28+
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+
30+
!!! note "Un-simplified system"
31+
32+
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.
33+
34+
## Generating an output function, ``g``
35+
36+
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.
37+
38+
The order of the user-specified output variables determines the order of the output vector ``y``.
39+
40+
## Input-output variable metadata
41+
42+
See [Symbolic Metadata](@ref symbolic_metadata). Metadata specified when creating variables is not directly used by any of the functions above, but the user can use the accessor functions `ModelingToolkit.inputs(sys)` and `ModelingToolkit.outputs(sys)` to obtain all variables with such metadata for passing to the functions above. The presence of this metadata is not required for any IO functionality and may be omitted.
43+
44+
## Linearization
45+
46+
See [Linearization](@ref linearization).
47+
48+
## Docstrings
49+
50+
```@index
51+
Pages = ["InputOutput.md"]
52+
```
53+
54+
```@docs
55+
ModelingToolkit.generate_control_function
56+
ModelingToolkit.build_explicit_observed_function
57+
```

docs/src/basics/Linearization.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ eqs = [u ~ kp * (r - y) # P controller
2929
D(x) ~ -x + u # First-order plant
3030
y ~ x] # Output equation
3131
32-
@named sys = ODESystem(eqs, t)
32+
@named sys = ODESystem(eqs, t) # Do not call @mtkbuild when linearizing
3333
matrices, simplified_sys = linearize(sys, [r], [y]) # Linearize from r to y
3434
matrices
3535
```
@@ -41,6 +41,14 @@ using ModelingToolkit: inputs, outputs
4141
[unknowns(simplified_sys); inputs(simplified_sys); outputs(simplified_sys)]
4242
```
4343

44+
!!! note "Inputs must be unconnected"
45+
46+
The model above has 4 variables but only three equations, there is no equation specifying the value of `r` since `r` is an input. This means that only unbalanced models can be linearized, or in other words, models that are balanced and can be simulated _cannot_ be linearized. To learn more about this, see https://www.youtube.com/watch?v=-XOux-2XDGI&t=395s. Also see [ModelingToolkitStandardLibrary: Linear analysis](https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/API/linear_analysis/) for utilities that make linearization of completed models easier.
47+
48+
!!! note "Un-simplified system"
49+
50+
Linearization expects `sys` to be un-simplified, i.e., `structural_simplify` or `@mtkbuild` should not be called on the system before linearizing.
51+
4452
## Operating point
4553

4654
The operating point to linearize around can be specified with the keyword argument `op` like this: `op = Dict(x => 1, r => 2)`. The operating point may include specification of unknown variables, input variables and parameters. For variables that are not specified in `op`, the default value specified in the model will be used if available, if no value is specified, an error is thrown.
@@ -69,6 +77,8 @@ If the modeled system is actually proper (but MTK failed to find a proper realiz
6977

7078
[ModelingToolkitStandardLibrary](https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/) contains a set of [tools for more advanced linear analysis](https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/API/linear_analysis/). These can be used to make it easier to work with and analyze causal models, such as control and signal-processing systems.
7179

80+
Also see [ControlSystemsMTK.jl](https://juliacontrol.github.io/ControlSystemsMTK.jl/dev/) for an interface to [ControlSystems.jl](https://github.com/JuliaControl/ControlSystems.jl) that contains tools for linear analysis and frequency-domain analysis.
81+
7282
## Docstrings
7383

7484
```@index

src/inputoutput.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ The return values also include the remaining unknowns and parameters, in the ord
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.
182182
183+
!!! note "Un-simplified system"
184+
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.
185+
183186
# Example
184187
185188
```

0 commit comments

Comments
 (0)