Skip to content
FirstOrderTest.md

FirstOrderTest

Test fixture for evaluating first-order system response to constant input.

This component connects a constant value (1.0) to a first-order system with gain 1.2 and time constant 0.1, allowing observation of the step response. The first-order system is expected to reach its steady state value of 1.2 after approximately 5 time constants (0.5 seconds). The metadata includes a test case that verifies this expected behavior after 10 seconds of simulation.

Usage

FirstOrderTest()

Behavior

c.y(t)=pt1.u(t)c.y(t)=c.kdpt1.x(t)dt=pt1.x(t)+pt1.kpt1.u(t)pt1.Tpt1.y(t)=pt1.x(t)

Source

dyad
# Test fixture for evaluating first-order system response to constant input.
#
# This component connects a constant value (1.0) to a first-order system with gain 1.2 and
# time constant 0.1, allowing observation of the step response. The first-order system is
# expected to reach its steady state value of 1.2 after approximately 5 time constants (0.5 seconds).
# The metadata includes a test case that verifies this expected behavior after 10 seconds of simulation.
test component FirstOrderTest
  # Constant block that provides a unit step input with value k=1
  c = Constant(k=1)
  # First-order system with gain 1.2 and time constant 0.1 seconds
  pt1 = FirstOrder(k=1.2, T=0.1)
relations
  # Connects the constant output to the first-order system input
  connect(c.y, pt1.u)
metadata {
  "Dyad": {
    "experiments": {},
    "tests": {
      "case1": {
        "stop": 10,
        "atol": {"pt1.y": 0.001},
        "expect": {"final": {"pt1.y": 1.2}, "signals": ["pt1.y"]}
      }
    }
  }
}
end
Flattened Source
dyad
# Test fixture for evaluating first-order system response to constant input.
#
# This component connects a constant value (1.0) to a first-order system with gain 1.2 and
# time constant 0.1, allowing observation of the step response. The first-order system is
# expected to reach its steady state value of 1.2 after approximately 5 time constants (0.5 seconds).
# The metadata includes a test case that verifies this expected behavior after 10 seconds of simulation.
test component FirstOrderTest
  # Constant block that provides a unit step input with value k=1
  c = Constant(k=1)
  # First-order system with gain 1.2 and time constant 0.1 seconds
  pt1 = FirstOrder(k=1.2, T=0.1)
relations
  # Connects the constant output to the first-order system input
  connect(c.y, pt1.u)
metadata {
  "Dyad": {
    "experiments": {},
    "tests": {
      "case1": {
        "stop": 10,
        "atol": {"pt1.y": 0.001},
        "expect": {"final": {"pt1.y": 1.2}, "signals": ["pt1.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 = FirstOrderTest()
u0_case1 = []
prob_case1 = ODEProblem(model_case1, u0_case1, (0, 10))
sol_case1 = solve(prob_case1)
retcode: Success
Interpolation: 3rd order Hermite
t: 33-element Vector{Float64}:
  0.0
  9.999999999999999e-5
  0.0010999999999999998
  0.007643661441638699
  0.022505075556921522
  0.04447858236944112
  0.07261559353635375
  0.10885733084666237
  0.15314251261703263
  0.20676179774053446

  3.9287621493323477
  4.258450452945037
  4.594762911377889
  4.9402282143398155
  5.292692753046255
  6.003600709956894
  6.945885150744364
  8.831651597604731
 10.0
u: 33-element Vector{Vector{Float64}}:
 [-0.0]
 [0.00119940019995001]
 [0.013127665469557502]
 [0.08830603971090209]
 [0.24182917035867987]
 [0.4308461419242686]
 [0.6194819019674526]
 [0.7959640055586799]
 [0.9405267439089751]
 [1.0482147648158164]

 [1.1994597940016913]
 [1.1996319716399626]
 [1.1997167100274564]
 [1.1997422357291465]
 [1.1997338484352627]
 [1.2000544821933894]
 [1.1999888117427526]
 [1.2000017908013547]
 [1.1999996483770548]
julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.pt1.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "FirstOrderTest_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.pt1.y], width=2, label="Actual value of pt1.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of pt1.y")
end
scatter!(plt, [df_case1.t[end]], [1.2], label="Final Condition for `pt1.y`")

plt