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 ​

BlockComponents.FirstOrderTest()

Behavior ​

julia
using BlockComponents #hide
using ModelingToolkit #hide
@named sys = BlockComponents.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) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 70, "y1": 110, "x2": 170, "y2": 210, "rot": 0}
      }
    }
  }
  "First-order system with gain 1.2 and time constant 0.1 seconds"
  pt1 = FirstOrder(k = 1.2, T = 0.1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 310, "y1": 110, "x2": 410, "y2": 210, "rot": 0}
      }
    }
  }
relations
  "Connects the constant output to the first-order system input"
  connect(c.y, pt1.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  initial pt1.y = 0
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "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) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 70, "y1": 110, "x2": 170, "y2": 210, "rot": 0}
      }
    }
  }
  "First-order system with gain 1.2 and time constant 0.1 seconds"
  pt1 = FirstOrder(k = 1.2, T = 0.1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 310, "y1": 110, "x2": 410, "y2": 210, "rot": 0}
      }
    }
  }
relations
  "Connects the constant output to the first-order system input"
  connect(c.y, pt1.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  initial pt1.y = 0
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "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 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 = FirstOrderTest()
model_case1 = toggle_namespacing(model_case1, false)

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