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

c.y(t)=pt2.u(t)c.y(t)=c.kdpt2.x(t)dt=pt2.xd(t)dpt2.xd(t)dt=(2pt2.dpt2.xd(t)+(pt2.x(t)+pt2.kpt2.u(t))pt2.w)pt2.wpt2.y(t)=pt2.x(t)

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)
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)
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

This is setup code, that must be run before each test case.

julia
using BlockComponents
using ModelingToolkit, OrdinaryDiffEqDefault
using Plots
using CSV, DataFrames

snapshotsdir = joinpath(dirname(dirname(pathof(BlockComponents))), "test", "snapshots")
"/home/actions-runner-10/.julia/packages/BlockComponents/77kIK/test/snapshots"

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)
retcode: Success
Interpolation: 3rd order Hermite
t: 23-element Vector{Float64}:
  0.0
  9.999999999999999e-5
  0.0010999999999999998
  0.011099999999999997
  0.06011862644596693
  0.15042632519643986
  0.28380492936210605
  0.4678186118211677
  0.7152653512117267
  1.0347207106005918

  3.8238016789236675
  4.511767493047128
  5.20478873282933
  5.939653827614375
  6.737441224771508
  7.636538803204853
  8.455698732346535
  9.252425660114291
 10.0
u: 23-element Vector{Vector{Float64}}:
 [-0.0, 0.0]
 [4.9998333333333834e-9, 9.999500000000413e-5]
 [6.047781666800803e-7, 0.0010993950000609898]
 [6.137706290157373e-5, 0.011038395631125119]
 [0.0017709171509673284, 0.058312039562446724]
 [0.010747355599385366, 0.13913297859763982]
 [0.03647738129711265, 0.24378730937010135]
 [0.09253533051178131, 0.360201390056661]
 [0.19618910514100768, 0.4688260927491307]
 [0.3588950053617409, 0.5374947938657385]

 [1.160098956787946, -0.028849553664129345]
 [1.117466316373297, -0.08384692988675878]
 [1.0569695772902532, -0.08376886433971201]
 [1.0054577786449512, -0.053824533462138155]
 [0.977594775391023, -0.01724513854127507]
 [0.9751095000256216, 0.008216345939253489]
 [0.9853487202688161, 0.014516694787007045]
 [0.9959610324860069, 0.011165794886770332]
 [1.002166008436529, 0.0053890250122180725]
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`")

plt