Skip to content
LimiterTest.md

LimiterTest

Test harness for the Limiter component that constrains signals to specified bounds.

This test component creates a signal chain that feeds a high-amplitude sine wave through an integrator and then into a limiter with bounds at -3 and 3. The limiter constrains the integrated sine wave output to stay within the specified bounds, demonstrating how signal limiting works in practice. The test includes verification of expected output values.

Usage

LimiterTest()

Behavior

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

Source

dyad
# Test harness for the Limiter component that constrains signals to specified bounds.
#
# This test component creates a signal chain that feeds a high-amplitude sine wave through
# an integrator and then into a limiter with bounds at -3 and 3. The limiter constrains the
# integrated sine wave output to stay within the specified bounds, demonstrating how signal
# limiting works in practice. The test includes verification of expected output values.
test component LimiterTest
  # Limiter block that constraints signals between -3 and 3
  limiter = Limiter(y_max=3, y_min=-3)
  # Integrator block that accumulates the input signal
  integrator = Integrator()
  # Sine wave generator with large amplitude to drive the system
  sine = Sine(amplitude=4*π, frequency=1)
relations
  # Connect sine output to integrator input
  connect(sine.y, integrator.u)
  # Connect integrator output to limiter input
  connect(integrator.y, limiter.u)
metadata {
  "Dyad": {
    "tests": {
      "case1": {
        "stop": 1,
        "atol": {"limiter.y": 0.000001},
        "expect": {"signals": ["limiter.y"], "final": {"limiter.y": 0}}
      }
    }
  }
}
end
Flattened Source
dyad
# Test harness for the Limiter component that constrains signals to specified bounds.
#
# This test component creates a signal chain that feeds a high-amplitude sine wave through
# an integrator and then into a limiter with bounds at -3 and 3. The limiter constrains the
# integrated sine wave output to stay within the specified bounds, demonstrating how signal
# limiting works in practice. The test includes verification of expected output values.
test component LimiterTest
  # Limiter block that constraints signals between -3 and 3
  limiter = Limiter(y_max=3, y_min=-3)
  # Integrator block that accumulates the input signal
  integrator = Integrator()
  # Sine wave generator with large amplitude to drive the system
  sine = Sine(amplitude=4*π, frequency=1)
relations
  # Connect sine output to integrator input
  connect(sine.y, integrator.u)
  # Connect integrator output to limiter input
  connect(integrator.y, limiter.u)
metadata {
  "Dyad": {
    "tests": {
      "case1": {
        "stop": 1,
        "atol": {"limiter.y": 0.000001},
        "expect": {"signals": ["limiter.y"], "final": {"limiter.y": 0}}
      }
    }
  }
}
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 = LimiterTest()
u0_case1 = []
prob_case1 = ODEProblem(model_case1, u0_case1, (0, 1))
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.limiter.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "LimiterTest_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.limiter.y], width=2, label="Actual value of limiter.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of limiter.y")
end
scatter!(plt, [df_case1.t[end]], [0], label="Final Condition for `limiter.y`")
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>