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

SecondOrderTest()

Behavior

julia
using BlockComponents #hide
using ModelingToolkit #hide
@named sys = 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)
  # Second-order transfer function block with specified dynamics parameters
  pt2 = SecondOrder(k=1.0, w=1.0, d=0.5)
relations
  # Connects the constant output to the input of the second-order system
  connect(c.y, pt2.u)
  initial pt2.x = 0
metadata {
  "Dyad": {
    "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)
  # Second-order transfer function block with specified dynamics parameters
  pt2 = SecondOrder(k=1.0, w=1.0, d=0.5)
relations
  # Connects the constant output to the input of the second-order system
  connect(c.y, pt2.u)
  initial pt2.x = 0
metadata {
  "Dyad": {
    "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 ModelingToolkit, OrdinaryDiffEqDefault
using Plots
using CSV, DataFrames

snapshotsdir = joinpath(dirname(dirname(pathof(BlockComponents))), "test", "snapshots")
<< @setup-block not executed in draft mode >>

Test Case case1

julia
@mtkbuild model_case1 = SecondOrderTest()
u0_case1 = [model_case1.pt2.xd => 0]
prob_case1 = ODEProblem(model_case1, u0_case1, (0, 10))
sol_case1 = solve(prob_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 >>