You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/tutorials/disturbance_modeling.md
+11-3Lines changed: 11 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -119,7 +119,15 @@ y &= g(x, u, p, t)
119
119
\end{aligned}
120
120
```
121
121
122
-
To make use of the model defined above for state estimation, we may want to generate a Julia function for the dynamics ``f`` and the output equations ``g`` that we can plug into, e.g., a nonlinear version of a Kalman filter or a particle filter, etc. MTK contains utilities to do this, namely, [`ModelingToolkit.generate_control_function`](@ref) and [`ModelingToolkit.build_explicit_observed_function`](@ref) (described in more details in ["Input output"](@ref inputoutput)). These functions take keyword arguments `disturbance_inputs` and `disturbance_argument`, that indicate which variables in the model are considered part of ``w``, and whether or not these variables are to be added as function arguments to ``f``, i.e., whether we have ``f(x, u, p, t)`` or ``f(x, u, p, t, w)``. If we do not include the disturbance inputs as function arguments, MTK will assume that the ``w`` variables are all zero, but any dynamics associated with these variables, such as disturbance models, will be included in the generated function. This allows a state estimator to estimate the state of the disturbance model, provided that this state is [observable](https://en.wikipedia.org/wiki/Observability) from the measured outputs of the system.
122
+
To make use of the model defined above for state estimation, we may want to generate a Julia function for the dynamics ``f`` and the output equations ``g`` that we can plug into, e.g., a nonlinear version of a Kalman filter or a particle filter, etc. MTK contains utilities to do this, namely, [`ModelingToolkit.generate_control_function`](@ref) and [`ModelingToolkit.build_explicit_observed_function`](@ref) (described in more details in ["Input output"](@ref inputoutput)).
123
+
124
+
These functions support two types of disturbance inputs:
125
+
126
+
-**Unknown disturbances** (`disturbance_inputs`): Variables that are part of ``w`` but NOT added as function arguments to ``f``. MTK assumes these variables are zero, but any dynamics associated with them (such as disturbance models) are included in the generated function. This allows a state estimator to estimate the state of the disturbance model, provided that this state is [observable](https://en.wikipedia.org/wiki/Observability) from measured outputs.
127
+
128
+
-**Known disturbances** (`known_disturbance_inputs`): Variables that are part of ``w`` AND added as function arguments to ``f``, resulting in ``f(x, u, p, t, w)``. Use this when disturbances can be measured or are otherwise known.
129
+
130
+
You can mix and match: some disturbances can be unknown while others are known. For example, `generate_control_function(sys, inputs; disturbance_inputs=[w1], known_disturbance_inputs=[w2, w3])` generates a function `f(x, u, p, t, [w2, w3])` where `w1` is set to zero but its dynamics are preserved, while `w2` and `w3` must be provided as arguments.
For a system `sys` with inputs (as determined by [`unbound_inputs`](@ref) or user specified), generate functions with additional input argument `u`
167
168
168
169
The returned functions are the out-of-place (`f_oop`) and in-place (`f_ip`) forms:
169
170
```
170
-
f_oop : (x,u,p,t) -> rhs
171
-
f_ip : (xout,x,u,p,t) -> nothing
171
+
f_oop : (x,u,p,t) -> rhs # basic form
172
+
f_oop : (x,u,p,t,w) -> rhs # with known_disturbance_inputs
173
+
f_ip : (xout,x,u,p,t) -> nothing # basic form
174
+
f_ip : (xout,x,u,p,t,w) -> nothing # with known_disturbance_inputs
172
175
```
173
176
174
177
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`.
175
178
176
-
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 (by default) 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. To add an input argument corresponding to the disturbance inputs, either include the disturbance inputs among the control inputs, or set `disturbance_argument=true`, in which case an additional input argument `w` is added to the generated function `(x,u,p,t,w)->rhs`.
179
+
# Disturbance Handling
180
+
181
+
- `disturbance_inputs`: Unknown disturbance inputs. The generated dynamics will preserve any state and dynamics associated with these disturbances, but the disturbance inputs themselves will not be included as function arguments. This is useful for state observers that estimate unmeasured disturbances.
182
+
183
+
- `known_disturbance_inputs`: Known disturbance inputs. The generated dynamics will preserve state and dynamics, and the disturbance inputs will be added as an additional input argument `w` to the generated function: `(x,u,p,t,w)->rhs`.
177
184
178
185
# Example
179
186
180
-
```
187
+
```julia
181
188
using ModelingToolkit: generate_control_function, varmap_to_vars, defaults
Copy file name to clipboardExpand all lines: src/systems/codegen.jl
+26-8Lines changed: 26 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -982,6 +982,8 @@ Generates a function that computes the observed value(s) `ts` in the system `sys
982
982
- `output_type = Array` the type of the array generated by a out-of-place vector-valued function
983
983
- `param_only = false` if true, only allow the generated function to access system parameters
984
984
- `inputs = nothing` additinoal symbolic variables that should be provided to the generated function
985
+
- `disturbance_inputs = nothing` symbolic variables representing unknown disturbance inputs (removed from parameters, not added as function arguments)
986
+
- `known_disturbance_inputs = nothing` symbolic variables representing known disturbance inputs (removed from parameters, added as function arguments)
985
987
- `checkbounds = true` checks bounds if true when destructuring parameters
986
988
- `op = Operator` sets the recursion terminator for the walk done by `vars` to identify the variables that appear in `ts`. See the documentation for `vars` for more detail.
987
989
- `throw = true` if true, throw an error when generating a function for `ts` that reference variables that do not exist.
@@ -1005,16 +1007,18 @@ The signatures will be of the form `g(...)` with arguments:
1005
1007
1006
1008
- `output` for in-place functions
1007
1009
- `unknowns` if `param_only` is `false`
1008
-
- `inputs` if `inputs` is an array of symbolic inputs that should be available in `ts`
1010
+
- `inputs` if `inputs` is an array of symbolic inputs that should be available in `ts`
1009
1011
- `p...` unconditionally; note that in the case of `MTKParameters` more than one parameters argument may be present, so it must be splatted
1010
1012
- `t` if the system is time-dependent; for example systems of nonlinear equations will not have `t`
1013
+
- `known_disturbance_inputs` if provided; these are disturbance inputs that are known and provided as arguments
1011
1014
1012
-
For example, a function `g(op, unknowns, p..., inputs, t)` will be the in-place function generated if `return_inplace` is true, `ts` is a vector,
1013
-
an array of inputs `inputs` is given, and `param_only` is false for a time-dependent system.
1015
+
For example, a function `g(op, unknowns, p..., inputs, t, known_disturbances)` will be the in-place function generated if `return_inplace` is true, `ts` is a vector,
1016
+
an array of inputs `inputs` is given, `known_disturbance_inputs` is provided, and `param_only` is false for a time-dependent system.
1014
1017
"""
1015
1018
functionbuild_explicit_observed_function(sys, ts;
1016
1019
inputs =nothing,
1017
1020
disturbance_inputs =nothing,
1021
+
known_disturbance_inputs =nothing,
1018
1022
disturbance_argument =false,
1019
1023
expression =false,
1020
1024
eval_expression =false,
@@ -1095,22 +1099,36 @@ function build_explicit_observed_function(sys, ts;
1095
1099
ps =setdiff(ps, inputs) # Inputs have been converted to parameters by io_preprocessing, remove those from the parameter list
1096
1100
inputs = (inputs,)
1097
1101
end
1102
+
# Handle backward compatibility for disturbance_argument
1103
+
if disturbance_argument
1104
+
Base.depwarn("The `disturbance_argument` keyword argument is deprecated. Use `known_disturbance_inputs` instead. "*
0 commit comments