StateSpaceTest ​
Test component for StateSpace block demonstrating basic functionality.
Creates a simple test system with:
A step input signal
A second-order state space system
Integration of the output for verification
The state space system represents a second-order system with:
States: position and velocity
Single input: force/acceleration
Single output: position
Operating point offset to test that feature
Usage ​
BlockComponents.StateSpaceTest()
Behavior ​
julia
using BlockComponents #hide
using ModelingToolkit #hide
@named sys = BlockComponents.StateSpaceTest() #hide
full_equations(sys) #hide<< @example-block not executed in draft mode >>Source ​
dyad
"""
Test component for StateSpace block demonstrating basic functionality.
Creates a simple test system with:
- A step input signal
- A second-order state space system
- Integration of the output for verification
The state space system represents a second-order system with:
- States: position and velocity
- Single input: force/acceleration
- Single output: position
- Operating point offset to test that feature
"""
component StateSpaceTest
"Step input signal starting at time 1.0 with amplitude 2.0"
step = BlockComponents.Step(height = 2.0, start_time = 1.0)
"""
Second-order state space system
Represents: d²x/dt² + 2*damping*freq*dx/dt + freq²*x = freq²*u
In state space form with x1=position, x2=velocity:
dx1/dt = x2
dx2/dt = -freq²*x1 - 2*damping*freq*x2 + freq²*u
y = x1 (output position)
"""
state_space = BlockComponents.StateSpace(nx = 2, nu = 1, ny = 1, A = reshape([0.0, 1.0, -4.0, -2.0], 2, 2), B = reshape([0.0, 4.0], 2, 1), C = reshape([1.0, 0.0], 1, 2), D = reshape([0.0], 1, 1), x0 = [0.5, 0.0], u0 = [0.5], y0 = [0.1]) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 180, "y1": 0, "x2": 280, "y2": 100, "rot": 0}
}
}
}
"Integrator to accumulate the output for testing"
integrator = BlockComponents.Integrator(k = 1.0, x0 = 0.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 360, "y1": 0, "x2": 460, "y2": 100, "rot": 0}
}
}
}
"Terminator for the integrated signal"
terminator = BlockComponents.Terminator() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 540, "y1": 0, "x2": 640, "y2": 100, "rot": 0}
}
}
}
relations
"Connect step input to state space system"
connect(step.y, state_space.u[1])
"Connect state space output to integrator"
connect(state_space.y[1], integrator.u)
"Connect integrator output to terminator"
connect(integrator.y, terminator.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
"Dyad": {
"icons": {"default": "dyad://BlockComponents/Example.svg"},
"tests": {
"case1": {
"stop": 5,
"rtol": {"integrator.y": 0.0001},
"expect": {
"signals": ["integrator.y"],
"initial": {"integrator.y": 0},
"final": {"integrator.y": -18.288754189661404}
}
}
}
}
}
endFlattened Source
dyad
"""
Test component for StateSpace block demonstrating basic functionality.
Creates a simple test system with:
- A step input signal
- A second-order state space system
- Integration of the output for verification
The state space system represents a second-order system with:
- States: position and velocity
- Single input: force/acceleration
- Single output: position
- Operating point offset to test that feature
"""
component StateSpaceTest
"Step input signal starting at time 1.0 with amplitude 2.0"
step = BlockComponents.Step(height = 2.0, start_time = 1.0)
"""
Second-order state space system
Represents: d²x/dt² + 2*damping*freq*dx/dt + freq²*x = freq²*u
In state space form with x1=position, x2=velocity:
dx1/dt = x2
dx2/dt = -freq²*x1 - 2*damping*freq*x2 + freq²*u
y = x1 (output position)
"""
state_space = BlockComponents.StateSpace(nx = 2, nu = 1, ny = 1, A = reshape([0.0, 1.0, -4.0, -2.0], 2, 2), B = reshape([0.0, 4.0], 2, 1), C = reshape([1.0, 0.0], 1, 2), D = reshape([0.0], 1, 1), x0 = [0.5, 0.0], u0 = [0.5], y0 = [0.1]) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 180, "y1": 0, "x2": 280, "y2": 100, "rot": 0}
}
}
}
"Integrator to accumulate the output for testing"
integrator = BlockComponents.Integrator(k = 1.0, x0 = 0.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 360, "y1": 0, "x2": 460, "y2": 100, "rot": 0}
}
}
}
"Terminator for the integrated signal"
terminator = BlockComponents.Terminator() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 540, "y1": 0, "x2": 640, "y2": 100, "rot": 0}
}
}
}
relations
"Connect step input to state space system"
connect(step.y, state_space.u[1])
"Connect state space output to integrator"
connect(state_space.y[1], integrator.u)
"Connect integrator output to terminator"
connect(integrator.y, terminator.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
"Dyad": {
"icons": {"default": "dyad://BlockComponents/Example.svg"},
"tests": {
"case1": {
"stop": 5,
"rtol": {"integrator.y": 0.0001},
"expect": {
"signals": ["integrator.y"],
"initial": {"integrator.y": 0},
"final": {"integrator.y": -18.288754189661404}
}
}
}
}
}
endTest Cases ​
julia
using BlockComponents
using DyadInterface: TransientAnalysis, rebuild_sol
using ModelingToolkit: toggle_namespacing, get_defaults, @named
using CSV, DataFrames, Plots
snapshotsdir = joinpath(dirname(dirname(pathof(BlockComponents))), "test", "snapshots")<< @setup-block not executed in draft mode >>Test Case case1 ​
julia
@named model_case1 = StateSpaceTest()
model_case1 = toggle_namespacing(model_case1, false)
model_case1 = toggle_namespacing(model_case1, true)
result_case1 = TransientAnalysis(; model = model_case1, alg = "auto", start = 0e+0, stop = 5e+0, abstol=1e-6, reltol=1e-6)
sol_case1 = rebuild_sol(result_case1)<< @setup-block not executed in draft mode >>julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.integrator.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "StateSpaceTest_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.integrator.y], width=2, label="Actual value of integrator.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of integrator.y")
end
scatter!(plt, [df_case1.t[1]], [0], label="Initial Condition for `integrator.y`")
scatter!(plt, [df_case1.t[end]], [-18.288754189661404], label="Final Condition for `integrator.y`")<< @setup-block not executed in draft mode >>julia
plt<< @example-block not executed in draft mode >>Related ​
Examples
Experiments
Analyses