Skip to content
SecondOrderTest.md

SecondOrderTest ​

Second-order system test with constant input.

Tests a second-order dynamic system by applying a constant input and verifying the expected step response. The second-order system is configured with a gain of 1.0, natural frequency of 1.0, and damping ratio of 0.5, which produces a slightly under-damped response. The system should eventually reach the steady-state output value matching the input constant.

Usage ​

BlockComponents.SecondOrderTest()

Behavior ​

julia
using BlockComponents #hide
using ModelingToolkit #hide
@named sys = BlockComponents.SecondOrderTest() #hide
full_equations(sys) #hide
<< @example-block not executed in draft mode >>

Source ​

dyad
"""
Second-order system test with constant input.

Tests a second-order dynamic system by applying a constant input and verifying the expected
step response. The second-order system is configured with a gain of 1.0, natural frequency
of 1.0, and damping ratio of 0.5, which produces a slightly under-damped response. The system
should eventually reach the steady-state output value matching the input constant.
"""
test component SecondOrderTest
  "Constant source block that provides a fixed value of 1 as input"
  c = Constant(k = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 70, "y1": 120, "x2": 170, "y2": 220, "rot": 0}
      }
    }
  }
  "Second-order transfer function block with specified dynamics parameters"
  pt2 = SecondOrder(k = 1.0, w = 1.0, d = 0.5) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 290, "y1": 120, "x2": 390, "y2": 220, "rot": 0}
      }
    }
  }
relations
  "Connects the constant output to the input of the second-order system"
  connect(c.y, pt2.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  initial pt2.x = 0
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "experiments": {},
    "tests": {
      "case1": {
        "stop": 10,
        "initial": {"pt2.xd": 0},
        "atol": {"pt2.y": 0.01},
        "expect": {"final": {"pt2.y": 1}, "signals": ["pt2.y"]}
      }
    }
  }
}
end
Flattened Source
dyad
"""
Second-order system test with constant input.

Tests a second-order dynamic system by applying a constant input and verifying the expected
step response. The second-order system is configured with a gain of 1.0, natural frequency
of 1.0, and damping ratio of 0.5, which produces a slightly under-damped response. The system
should eventually reach the steady-state output value matching the input constant.
"""
test component SecondOrderTest
  "Constant source block that provides a fixed value of 1 as input"
  c = Constant(k = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 70, "y1": 120, "x2": 170, "y2": 220, "rot": 0}
      }
    }
  }
  "Second-order transfer function block with specified dynamics parameters"
  pt2 = SecondOrder(k = 1.0, w = 1.0, d = 0.5) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 290, "y1": 120, "x2": 390, "y2": 220, "rot": 0}
      }
    }
  }
relations
  "Connects the constant output to the input of the second-order system"
  connect(c.y, pt2.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  initial pt2.x = 0
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "experiments": {},
    "tests": {
      "case1": {
        "stop": 10,
        "initial": {"pt2.xd": 0},
        "atol": {"pt2.y": 0.01},
        "expect": {"final": {"pt2.y": 1}, "signals": ["pt2.y"]}
      }
    }
  }
}
end


Test 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 = SecondOrderTest()
model_case1 = toggle_namespacing(model_case1, false)
get_defaults(model_case1)[model_case1.pt2.xd] = 0
model_case1 = toggle_namespacing(model_case1, true)
result_case1 = TransientAnalysis(; model = model_case1, alg = "auto", start = 0e+0, stop = 1e+1, 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.pt2.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "SecondOrderTest_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.pt2.y], width=2, label="Actual value of pt2.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of pt2.y")
end
scatter!(plt, [df_case1.t[end]], [1], label="Final Condition for `pt2.y`")
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>