API Reference
ModelingToolkitStandardLibrary.Blocks — Module
The module Blocks contains common input-output components, referred to as blocks.
ModelingToolkitStandardLibrary.Blocks.Add — Method
Add(; name, k1 = 1.0, k2 = 1.0)Output the sum of the two scalar inputs.
Parameters:
k1: Gain for first inputk2: Gain for second input
Connectors:
input1input2output
ModelingToolkitStandardLibrary.Blocks.Add3 — Method
Add(; name, k1 = 1.0, k2 = 1.0, k3 = 1.0)Output the sum of the three scalar inputs.
Parameters:
k1: Gain for first inputk2: Gain for second inputk3: Gain for third input
Connectors:
input1input2input3output
ModelingToolkitStandardLibrary.Blocks.Atan2 — Method
Atan2(; name)Output the arc tangent of the input.
Connectors:
input1input2output
ModelingToolkitStandardLibrary.Blocks.Ceil — Method
Ceil(; name)Output the ceiling rounding of the input.
Connectors:
inputoutput
ModelingToolkitStandardLibrary.Blocks.Constant — Method
Constant(; name, k = 0.0)Generate constant signal.
Parameters:
k: Constant output value
Connectors:
output
ModelingToolkitStandardLibrary.Blocks.ContinuousClock — Method
ContinuousClock(; name, offset = 0, start_time = 0)Generate current time signal.
Parameters:
offset: Offset of output signalstart_time: [s] Outputy = offsetfort < start_time
Connectors:
output
ModelingToolkitStandardLibrary.Blocks.Cosine — Method
Cosine(; name, frequency, amplitude = 1, phase = 0, offset = 0, start_time = 0,
smooth = false)Generate cosine signal.
Parameters:
frequency: [Hz] Frequency of cosine waveamplitude: Amplitude of cosine wavephase: [rad] Phase of cosine waveoffset: Offset of output signalstart_time: [s] Outputy = offsetfort < start_timesmooth: Iftrue, returns a smooth wave. Defaults tofalseIt uses a default smoothing factor ofδ=1e-5, but this can be changed by supplyingsmooth=δ.
Connectors:
output
ModelingToolkitStandardLibrary.Blocks.DeadZone — Method
DeadZone(; name, u_max, u_min = -u_max)The DeadZone block defines a region of zero output. If the input is within u_min ... u_max, the output is zero. Outside of this zone, the output is a linear function of the input with a slope of 1.
y▲
│ /
│ /
u_min │ /
─────|──┼──|───────► u
/ │ u_max
/ │
/ │Parameters:
u_max: Upper limit of dead zoneu_min: Lower limit of dead zone
Connectors:
inputoutput
ModelingToolkitStandardLibrary.Blocks.Derivative — Method
Derivative(; name, k = 1, T, x = 0.0)Outputs an approximate derivative of the input. The transfer function of this block is
k k ks
─ - ─────── = ──────
T sT² + T sT + 1and a state-space realization is given by ss(-1/T, 1/T, -k/T, k/T) where T is the time constant of the filter. A smaller T leads to a more ideal approximation of the derivative.
Initial value of the state $x$ can be set with x.
Parameters:
k: GainT: [s] Time constant (T>0 required; T=0 is ideal derivative block)
Unknowns:
x: Unknown of Derivative. Defaults to 0.0.
Connectors:
inputoutput
ModelingToolkitStandardLibrary.Blocks.Division — Method
Division(; name)Output first input divided by second input.
Connectors:
input1input2output
ModelingToolkitStandardLibrary.Blocks.ExpSine — Method
ExpSine(; name, frequency, amplitude = 1, damping = 0.1, phase = 0, offset = 0, start_time = 0, smooth = false)Exponentially damped sine signal.
Parameters:
frequency: [Hz] Frequency of sine waveamplitude: Amplitude of sine wavedamping: [1/s] Damping coefficient of sine wavephase: [rad] Phase of sine waveoffset: Offset of output signalstart_time: [s] Outputy = offsetfort < start_timesmooth: Iftrue, returns a smooth wave. Defaults tofalseIt uses a default smoothing factor ofδ=1e-5, but this can be changed by supplyingsmooth=δ.
Connectors:
output
ModelingToolkitStandardLibrary.Blocks.Feedback — Method
Feedback(; name)Output difference between reference input (input1) and feedback input (input2).
Connectors:
input1input2output
ModelingToolkitStandardLibrary.Blocks.FirstOrder — Method
FirstOrder(; name, k = 1.0, T, x = 0.0, lowpass = true)A first-order filter with a single real pole at s = -1/T and gain k. If lowpass=true (default), the transfer function is given by $Y(s)/U(s) =$
k
───────
sT + 1and if lowpass=false, by
sT + 1 - k
──────────
sT + 1Initial value of the state x can be set with x
Parameters:
k: GainT: [s] Time constant (T>0 required)
Connectors:
inputoutput
See also SecondOrder
ModelingToolkitStandardLibrary.Blocks.Floor — Method
Floor(; name)Output the floor rounding of the input.
Connectors:
inputoutput
ModelingToolkitStandardLibrary.Blocks.Gain — Method
Gain(; name, k)Output the product of a gain value with the input signal.
Parameters:
k: Scalar gain
Connectors:
inputoutput
ModelingToolkitStandardLibrary.Blocks.Integrator — Method
Integrator(;name, k = 1, x = 0.0)Outputs y = ∫k*u dt, corresponding to the transfer function $1/s$. Initial value of integrator state $x$ can be set with x
Connectors:
inputoutput
Parameters:
k: Gain of integrator
Unknowns:
x: State of Integrator. Defaults to 0.0.
ModelingToolkitStandardLibrary.Blocks.Interpolation — Method
Interpolation(interp_type, u, x, args...; name)Represent function interpolation symbolically as a block component. By default interpolation types from DataInterpolations.jl are supported, but in general any callable type that builds the interpolation object via itp = interpolation_type(u, x, args...) and calls the interpolation with itp(t) should work. This does not need to represent an interpolation, it can be any type that satisfies the interface, such as lookup tables.
Arguments:
interp_type: the type of the interpolation. ForDataInterpolations,
these would be any of the available interpolations, such as LinearInterpolation, ConstantInterpolation or CubicSpline.
u: the data used for interpolation. ForDataInterpolationsthis will be anAbstractVectorx: the values that each data points correspond to, usually the times corresponding to each value inu.args: any other arguments needed to build the interpolation
Keyword arguments:
name: the name of the component
Parameters:
interpolator: the symbolic representation of the interpolation object, callable asinterpolator(t)
Connectors:
input: aRealInputconnector corresponding to the input variableoutput: aRealOutputconnector corresponding to the interpolated value
ModelingToolkitStandardLibrary.Blocks.LimPI — Method
LimPI(; name, k = 1.0, T, Ta, int__x = 0.0, u_max = 1.0, u_min = -u_max)Text-book version of a PI-controller with actuator saturation and anti-windup measure.
The PI controller is implemented on standard form
\[u(t) = sat(k (e(t) + ∫\dfrac{1}{T}e(t) dt) )\]
The simplified expression above is given without the anti-windup protection.
Parameters:
k: Proportional gainT: [s] Integrator time constant (T>0 required)Ta: [s] Tracking time constant (Ta>0 required)
Connectors:
err_inputctr_output
ModelingToolkitStandardLibrary.Blocks.LimPID — Method
LimPID(; k, Ti=false, Td=false, wp=1, wd=1, Ni, Nd=12, u_max=Inf, u_min=-u_max, gains = false, name)Proportional-Integral-Derivative (PID) controller with output saturation, set-point weighting and integrator anti-windup.
The equation for the control signal is roughly
k(ep + 1/Ti * ∫e + Td * d/dt(ed))
e = u_r - u_y
ep = wp*u_r - u_y
ed = wd*u_r - u_ywhere the transfer function for the derivative includes additional filtering, see ? Derivative for more details.
Parameters:
k: Proportional gainTi: [s] Integrator time constant. Set tofalseto turn off integral action.Td: [s] Derivative time constant. Set tofalseto turn off derivative action.wp: [0,1] Set-point weighting in the proportional part.wd: [0,1] Set-point weighting in the derivative part.Nd: [1/s] Derivative limit, limits the derivative gain toNd*Td. Reasonable values are ∈ [8, 20]. A higher value gives a better approximation of an ideal derivative at the expense of higher noise amplification.Ni:Ni*Ticontrols the time constantTaof anti-windup tracking. A common (default) choice isTa = √(Ti*Td)which is realized byNi = √(Td / Ti). Anti-windup can be effectively turned off by settingNi = Inf.gains: Ifgains = true,TiandTdwill be interpreted as gains with a fundamental PID transfer function on parallel formki=Ti, kd=Td, k + ki/s + kd*s.
Connectors:
referencemeasurementctr_output
ModelingToolkitStandardLibrary.Blocks.Limiter — Method
Limiter(;name, y_max, y_min = y_max > 0 ? -y_max : -Inf)Limit the range of a signal.
Parameters:
y_max: Maximum of output signaly_min: Minimum of output signal
Connectors:
inputoutput
ModelingToolkitStandardLibrary.Blocks.MatrixGain — Method
MatrixGain(; K::AbstractArray, name)Output the product of a gain matrix with the input signal vector.
Structural parameters:
K: Matrix gain
Connectors:
inputoutput
ModelingToolkitStandardLibrary.Blocks.Modulo — Method
Modulo(; name)Output the remainder when the first input is divided by second input.
Connectors:
dividenddivisorremainder
ModelingToolkitStandardLibrary.Blocks.PI — Method
PI(;name, k = 1.0, T = 1.0, int.x = 0.0)Textbook version of a PI-controller without actuator saturation and anti-windup measure. The proportional gain can be set with k Initial value of integrator state x can be set with int.x
The PI controller is implemented on standard form:
\[U(s) = k (1 + \dfrac{1}{sT}) E(S)\]
Parameters:
k: Proportional gainT: [s] Integrator time constant (T>0 required)
Connectors:
err_inputctr_output
See also LimPI
ModelingToolkitStandardLibrary.Blocks.PID — Method
PID(;name, k=1, Ti=false, Td=false, Nd=10, int__x=0, der__x=0)Text-book version of a PID-controller without actuator saturation and anti-windup measure.
Parameters:
k: GainTi: [s] Integrator time constant (Ti>0 required). If set to false, no integral action is used.Td: [s] Derivative time constant (Td>0 required). If set to false, no derivative action is used.Nd: [s] Maximum derivative gain (inverse of filter time constant, Nd>0 required; Nd=0 is ideal derivative).int__x: Initial value for the integrator.der__x: Initial value for the derivative state.
Connectors:
err_inputctr_output
See also LimPID
ModelingToolkitStandardLibrary.Blocks.ParametrizedInterpolation — Method
ParametrizedInterpolation(interp_type, u, x, args...; name, t = ModelingToolkitBase.t_nounits)Represent function interpolation symbolically as a block component, with the interpolation data represented parametrically. By default interpolation types from DataInterpolations.jl are supported, but in general any callable type that builds the interpolation object via itp = interpolation_type(u, x, args...) and calls the interpolation with itp(t) should work. This does not need to represent an interpolation, it can be any type that satisfies the interface, such as lookup tables.
Arguments:
interp_type: the type of the interpolation. ForDataInterpolations,
these would be any of the available interpolations, such as LinearInterpolation, ConstantInterpolation or CubicSpline.
u: the data used for interpolation. ForDataInterpolationsthis will be anAbstractVectorx: the values that each data points correspond to, usually the times corresponding to each value inu.args: any other arguments beeded to build the interpolation
Keyword arguments:
name: the name of the component
Parameters:
data: the symbolic representation of the data passed at construction time viau.ts: the symbolic representation of times corresponding to the data passed at construction time viax.
Connectors:
input: aRealInputconnector corresponding to the independent variableoutput: aRealOutputconnector corresponding to the interpolated value
ModelingToolkitStandardLibrary.Blocks.Power — Method
Power(; name)Output the exponential with base as the first input and exponent as second input i.e u1^u2
Connectors:
baseexponentoutput
ModelingToolkitStandardLibrary.Blocks.Product — Method
Product(; name)Output product of the two inputs.
Connectors:
input1input2output
ModelingToolkitStandardLibrary.Blocks.Ramp — Method
Ramp(; name, height = 1, duration = 1, offset = 0, start_time = 0, smooth = false)
Generate ramp signal.
Parameters:
height: Height of rampduration: [s] Duration of ramp (= 0.0 gives a Step)offset: Offset of output signalstart_time: [s] Outputy = offsetfort < start_timesmooth: Iftrue, returns a smooth wave. Defaults tofalseIt uses a default smoothing factor ofδ=1e-5, but this can be changed by supplyingsmooth=δ.
Connectors:
output
ModelingToolkitStandardLibrary.Blocks.RealInput — Method
RealInput(; name, guess)Connector with one input signal of type Real.
Parameters:
guess=0: Guess value foru.
States:
u: Value of the connector which is a scalar.
ModelingToolkitStandardLibrary.Blocks.RealInputArray — Method
RealInputArray(; name, nin, guess)Connector with an array of input signals of type Real.
Parameters:
nin: Number of inputs.guess=zeros(nin): Guess value foru.
States:
u: Value of the connector which is an array.
ModelingToolkitStandardLibrary.Blocks.RealOutput — Method
RealOutput(; name, guess)Connector with one output signal of type Real.
Parameters:
guess=0: Guess value foru.
States:
u: Value of the connector which is a scalar.
ModelingToolkitStandardLibrary.Blocks.RealOutputArray — Method
RealOutputArray(; name, nout, guess)Connector with an array of output signals of type Real.
Parameters:
nout: Number of outputs.guess=zeros(nout): Guess value foru.
States:
u: Value of the connector which is an array.
ModelingToolkitStandardLibrary.Blocks.SISO — Method
SISO(;name, u_start = 0.0, y_start = 0.0)Single input single output (SISO) continuous system block.
Parameters:
u_start: Initial value for the inputy_start: Initial value for the output
ModelingToolkitStandardLibrary.Blocks.SampledData — Method
SampledData(; name, buffer)data input component.
Parameters:
buffer: aParametertype which holds the data and sample time
Connectors:
output
ModelingToolkitStandardLibrary.Blocks.SampledData — Method
SampledData(; name, buffer, sample_time, circular_buffer=true)data input component.
Parameters:
buffer::Vector{Real}: holds the data sampled atsample_timesample_time::Realcircular_buffer::Bool = true: how to handlet > length(buffer)*sample_time. If true data is considered circular, otherwise last data point is held.
Connectors:
output
ModelingToolkitStandardLibrary.Blocks.SecondOrder — Method
SecondOrder(; name, k = 1.0, w = 1.0, d = 1.0, x = 0.0, xd = 0.0)A second-order filter with gain k, a bandwidth of w rad/s and relative damping d. The transfer function is given by Y(s)/U(s) =
k*w^2
─────────────────
s² + 2d*w*s + w^2Critical damping corresponds to d=1, which yields the fastest step response without overshoot, d < 1 results in an underdamped filter while d > 1 results in an overdamped filter. d = 1/√2 corresponds to a Butterworth filter of order 2 (maximally flat frequency response). Initial value of the state x can be set with x, and of derivative state xd with xd.
Parameters:
k: Gainw: [rad/s] Angular frequencyd: Damping
Connectors:
inputoutput
ModelingToolkitStandardLibrary.Blocks.Sine — Method
Sine(; name, frequency, amplitude = 1, phase = 0, offset = 0, start_time = 0,
smooth = false)Generate sine signal.
Parameters:
frequency: [Hz] Frequency of sine waveamplitude: Amplitude of sine wavephase: [rad] Phase of sine waveoffset: Offset of output signalstart_time: [s] Outputy = offsetfort < start_timesmooth: Iftrue, returns a smooth wave. Defaults tofalseIt uses a default smoothing factor ofδ=1e-5, but this can be changed by supplyingsmooth=δ.
Connectors:
output
ModelingToolkitStandardLibrary.Blocks.SlewRateLimiter — Method
SlewRateLimiter(; name, y_start, rising = 1.0, falling = -rising, Td = 0.001)Limits the slew rate of a signal. Initial value of state Y can be set with int.y
Parameters:
rising: Maximum rising slew ratefalling: Maximum falling slew rateTd: [s] Derivative time constanty_start: Initial value ofystate of SISO
Connectors:
inputoutput
ModelingToolkitStandardLibrary.Blocks.Sqrt — Method
Sqrt(; name)Output the square root of the input (input >= 0 required).
Connectors:
ModelingToolkitStandardLibrary.Blocks.Square — Method
Square(; name, frequency = 1.0, amplitude = 1.0, offset = 0.0, start_time = 0.0,
smooth = false)Generate smooth square signal.
Parameters:
frequency: [Hz] Frequency of square waveamplitude: Amplitude of square waveoffset: Offset of output signalstart_time: [s] Outputy = offsetfort < start_timesmooth: Iftrue, returns a smooth wave. Defaults tofalseIt uses a default smoothing factor ofδ=1e-5, but this can be changed by supplyingsmooth=δ.
Connectors:
output
ModelingToolkitStandardLibrary.Blocks.StateSpace — Method
StateSpace(A, B, C, D = 0; x = zeros(size(A,1)), u0 = zeros(size(B,2)), y0 = zeros(size(C,1)), name)A linear, time-invariant state-space system on the form.
\[\begin{aligned} ẋ &= Ax + Bu \\ y &= Cx + Du \end{aligned}\]
Transfer functions can also be simulated by converting them to a StateSpace form.
y0 and u0 can be used to set an operating point, providing them changes the dynamics from an LTI system to the affine system
\[\begin{aligned} ẋ &= Ax + B(u - u0) \\ y &= Cx + D(u - u0) + y0 \end{aligned}\]
For a nonlinear system
\[\begin{aligned} ẋ &= f(x, u) \\ y &= h(x, u) \end{aligned}\]
linearized around the operating point x₀, u₀, we have y0, u0 = h(x₀, u₀), u₀.
ModelingToolkitStandardLibrary.Blocks.Step — Method
Step(;name, height=1, offset=0, start_time=0, duration=Inf, smooth=true)Generate step signal.
Parameters:
height: Height of stepoffset: Offset of output signalstart_time: [s] Outputy = offsetfort < start_timeand thereafteroffset+height.duration: [s] Ifduration < Infis supplied, the output will revert tooffsetafterdurationseconds.smooth: Iftrue, returns a smooth wave. Defaults totrueIt uses a default smoothing factor ofδ=1e-5, but this can be changed by supplyingsmooth=δ.
Connectors:
output
ModelingToolkitStandardLibrary.Blocks.Sum — Method
Sum(; input__nin::Int, name)Output the sum of the elements of the input port vector. Input port dimension can be set with input__nin
Connectors:
inputoutput
ModelingToolkitStandardLibrary.Blocks.TimeVaryingFunction — Method
TimeVaryingFunction(f; name)Outputs $f(t)$.
The input variable t can be changed by passing a different variable as the keyword argument t.
Connectors:
output
ModelingToolkitStandardLibrary.Blocks.TransferFunction — Method
TransferFunction(; b, a, name)A single input, single output, linear time-invariant system provided as a transfer-function.
Y(s) = b(s) / a(s) U(s)where b and a are vectors of coefficients of the numerator and denominator polynomials, respectively, ordered such that the coefficient of the highest power of s is first.
The internal state realization is on controller canonical form, with state variable x, output variable y and input variable u. For numerical robustness, the realization used by the integrator is scaled by the last entry of the a parameter. The internally scaled state variable is available as x_scaled.
To set the initial state, it's recommended to set the initial condition for x, and let that of x_scaled be computed automatically.
Parameters:
b: Numerator polynomial coefficients, e.g.,2s + 3is specified as[2, 3]a: Denominator polynomial coefficients, e.g.,s² + 2ωs + ω^2is specified as[1, 2ω, ω^2]
Connectors:
inputoutput
See also StateSpace which handles MIMO systems, as well as ControlSystemsMTK.jl for an interface between ControlSystems.jl and ModelingToolkitBase.jl for advanced manipulation of transfer functions and linear statespace systems. For linearization, see ModelingToolkit.linearize and Linear Analysis.
ModelingToolkitStandardLibrary.Blocks.Triangular — Method
Triangular(; name, amplitude = 1.0, frequency = 1.0, offset = 0.0,
start_time = 0.0, smooth = false)Generate smooth triangular signal for frequencies less than or equal to 25 Hz
Parameters:
frequency: [Hz] Frequency of square waveamplitude: Amplitude of square waveoffset: Offset of output signal.start_time: [s] Outputy = offsetfort < start_timesmooth: Iftrue, returns a smooth wave. Defaults tofalseIt uses a default smoothing factor ofδ=1e-5, but this can be changed by supplyingsmooth=δ.
Connectors:
output
ModelingToolkitStandardLibrary.Blocks.UnaryMinus — Method
UnaryMinus(; name)Output the product of -1 and the input.
Connectors:
inputoutput