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

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

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)
  initial pt1.y = 0
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)
  initial pt1.y = 0
metadata {
  "Dyad": {
    "experiments": {},
    "tests": {
      "case1": {
        "stop": 10,
        "atol": {"pt1.y": 0.001},
        "expect": {"final": {"pt1.y": 1.2}, "signals": ["pt1.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 = FirstOrderTest()
u0_case1 = []
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.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`")
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>