Skip to content
SlewRateLimiterTest.md

SlewRateLimiterTest

Test component that validates SlewRateLimiter behavior with a sinusoidal input.

This test component connects a sine wave generator to a slew rate limiter and verifies the expected output value after simulation. The slew rate limiter restricts how quickly the output signal can change, with separate limits for rising and falling rates. The sine input provides a continuously changing signal that exercises both the rising and falling rate limits.

Usage

SlewRateLimiterTest()

Behavior

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

Source

dyad
# Test component that validates SlewRateLimiter behavior with a sinusoidal input.
#
# This test component connects a sine wave generator to a slew rate limiter and verifies the expected
# output value after simulation. The slew rate limiter restricts how quickly the output signal can change,
# with separate limits for rising and falling rates. The sine input provides a continuously changing signal
# that exercises both the rising and falling rate limits.
test component SlewRateLimiterTest
  # Slew rate limiter instance with rising/falling limits and small time delay
  limiter = SlewRateLimiter(rising=1, falling=-1, td=0.001)
  # Sine wave generator producing a 2Hz signal with amplitude 2
  sine = Sine(amplitude=2, frequency=2)
relations
  # Connects the sine wave output to the slew rate limiter input
  connect(sine.y, limiter.u)
metadata {
  "Dyad": {
    "tests": {
      "case1": {
        "stop": 1,
        "expect": {"signals": ["limiter.y"], "final": {"limiter.y": -0.03277527668771871}}
      }
    }
  }
}
end
Flattened Source
dyad
# Test component that validates SlewRateLimiter behavior with a sinusoidal input.
#
# This test component connects a sine wave generator to a slew rate limiter and verifies the expected
# output value after simulation. The slew rate limiter restricts how quickly the output signal can change,
# with separate limits for rising and falling rates. The sine input provides a continuously changing signal
# that exercises both the rising and falling rate limits.
test component SlewRateLimiterTest
  # Slew rate limiter instance with rising/falling limits and small time delay
  limiter = SlewRateLimiter(rising=1, falling=-1, td=0.001)
  # Sine wave generator producing a 2Hz signal with amplitude 2
  sine = Sine(amplitude=2, frequency=2)
relations
  # Connects the sine wave output to the slew rate limiter input
  connect(sine.y, limiter.u)
metadata {
  "Dyad": {
    "tests": {
      "case1": {
        "stop": 1,
        "expect": {"signals": ["limiter.y"], "final": {"limiter.y": -0.03277527668771871}}
      }
    }
  }
}
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 = SlewRateLimiterTest()
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, "SlewRateLimiterTest_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.03277527668771871], label="Final Condition for `limiter.y`")
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>