LIBRARY
Nonlinear.Tests.VariableLimiter
Test harness for the VariableLimiter block.
A sine (amplitude 2, frequency 1) is limited by two configurations, both driven by the same source:
vl_tv— time-varying upper limit:limit1is a ramp falling from 1.5 to 0.5 over one second,limit2is a constant -1. This exercises the "variable" aspect: the upper clamp boundary moves during the run, so the saturated output tracks the ramping limit rather than a fixed value.vl_const— constant symmetric limitslimit1 = +1,limit2 = -1(the MSL baseline). Output passes through while|u| <= 1and clamps to +/-1 outside.
limit1 >= limit2 holds at all times in both cases (the block asserts this).
Usage
BlockComponents.Nonlinear.Tests.VariableLimiter()
Behavior
julia
using BlockComponents #hide
using ModelingToolkit #hide
@named sys = BlockComponents.Nonlinear.Tests.VariableLimiter() #hide
let eqs = full_equations(sys); Base.length(eqs) > 25 ? nothing : eqs end #hide<< @example-block not executed in draft mode >>Source
dyad
"""
Test harness for the VariableLimiter block.
A sine (amplitude 2, frequency 1) is limited by two configurations, both driven
by the same source:
- `vl_tv` — **time-varying** upper limit: `limit1` is a ramp falling from 1.5 to
0.5 over one second, `limit2` is a constant -1. This exercises the "variable"
aspect: the upper clamp boundary moves during the run, so the saturated output
tracks the ramping limit rather than a fixed value.
- `vl_const` — constant symmetric limits `limit1 = +1`, `limit2 = -1` (the MSL
baseline). Output passes through while `|u| <= 1` and clamps to +/-1 outside.
`limit1 >= limit2` holds at all times in both cases (the block asserts this).
"""
test component VariableLimiter
"Sine sweeping beyond the limits"
sine = BlockComponents.Sources.Sine(amplitude = 2, frequency = 1) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 120, "x2": 120, "y2": 220, "rot": 0}
}
}
}
"Time-varying upper limit: 1.5 -> 0.5 over [0, 1]"
upperRamp = BlockComponents.Sources.Ramp(offset = 1.5, height = -1.0, duration = 1.0, start_time = 0.0) {"Dyad": {"placement": {"diagram": {"x1": 20, "y1": 0, "x2": 120, "y2": 100}}}}
"Constant lower limit for the time-varying case"
lowerConst = BlockComponents.Sources.Constant(k = -1.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 260, "x2": 120, "y2": 360, "rot": 0}
}
}
}
"VariableLimiter with a moving upper limit"
vl_tv = BlockComponents.Nonlinear.VariableLimiter() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 260, "y1": 120, "x2": 360, "y2": 220, "rot": 0}
}
}
}
"Constant upper limit (+1)"
upperConst = BlockComponents.Sources.Constant(k = 1.0) {
"Dyad": {"placement": {"diagram": {"x1": 10, "y1": 450, "x2": 110, "y2": 550}}}
}
"Constant lower limit (-1)"
lowerConst2 = BlockComponents.Sources.Constant(k = -1.0) {
"Dyad": {"placement": {"diagram": {"x1": 10, "y1": 680, "x2": 110, "y2": 780}}}
}
"VariableLimiter with constant symmetric limits (baseline)"
vl_const = BlockComponents.Nonlinear.VariableLimiter() {
"Dyad": {"placement": {"diagram": {"x1": 260, "y1": 580, "x2": 360, "y2": 680}}}
}
relations
connect(sine.y, vl_tv.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(upperRamp.y, vl_tv.limit1) {
"Dyad": {"edges": [{"S": 1, "M": [{"x": 230, "y": 50}, {"x": 230, "y": 140}], "E": 2}]}
}
connect(lowerConst.y, vl_tv.limit2) {
"Dyad": {"edges": [{"S": 1, "M": [{"x": 230, "y": 310}, {"x": 230, "y": 200}], "E": 2}]}
}
connect(sine.y, vl_const.u) {
"Dyad": {"edges": [{"S": 1, "M": [{"x": 200, "y": 170}, {"x": 200, "y": 630}], "E": 2}]}
}
connect(upperConst.y, vl_const.limit1) {
"Dyad": {"edges": [{"S": 1, "M": [{"x": 230, "y": 500}, {"x": 230, "y": 600}], "E": 2}]}
}
connect(lowerConst2.y, vl_const.limit2) {
"Dyad": {
"edges": [
{
"S": 1,
"M": [
{"x": 110, "y": 620},
{"x": 110, "y": 730},
{"x": 215, "y": 730},
{"x": 215, "y": 660}
],
"E": 2
}
]
}
}
metadata {
"Dyad": {
"icons": {"default": "dyad://BlockComponents/Example.svg"},
"tests": {
"case1": {
"stop": 1,
"expect": {"signals": ["sine.y", "upperRamp.y", "vl_tv.y", "vl_const.y"]}
}
}
}
}
endFlattened Source
dyad
"""
Test harness for the VariableLimiter block.
A sine (amplitude 2, frequency 1) is limited by two configurations, both driven
by the same source:
- `vl_tv` — **time-varying** upper limit: `limit1` is a ramp falling from 1.5 to
0.5 over one second, `limit2` is a constant -1. This exercises the "variable"
aspect: the upper clamp boundary moves during the run, so the saturated output
tracks the ramping limit rather than a fixed value.
- `vl_const` — constant symmetric limits `limit1 = +1`, `limit2 = -1` (the MSL
baseline). Output passes through while `|u| <= 1` and clamps to +/-1 outside.
`limit1 >= limit2` holds at all times in both cases (the block asserts this).
"""
test component VariableLimiter
"Sine sweeping beyond the limits"
sine = BlockComponents.Sources.Sine(amplitude = 2, frequency = 1) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 120, "x2": 120, "y2": 220, "rot": 0}
}
}
}
"Time-varying upper limit: 1.5 -> 0.5 over [0, 1]"
upperRamp = BlockComponents.Sources.Ramp(offset = 1.5, height = -1.0, duration = 1.0, start_time = 0.0) {"Dyad": {"placement": {"diagram": {"x1": 20, "y1": 0, "x2": 120, "y2": 100}}}}
"Constant lower limit for the time-varying case"
lowerConst = BlockComponents.Sources.Constant(k = -1.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 260, "x2": 120, "y2": 360, "rot": 0}
}
}
}
"VariableLimiter with a moving upper limit"
vl_tv = BlockComponents.Nonlinear.VariableLimiter() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 260, "y1": 120, "x2": 360, "y2": 220, "rot": 0}
}
}
}
"Constant upper limit (+1)"
upperConst = BlockComponents.Sources.Constant(k = 1.0) {
"Dyad": {"placement": {"diagram": {"x1": 10, "y1": 450, "x2": 110, "y2": 550}}}
}
"Constant lower limit (-1)"
lowerConst2 = BlockComponents.Sources.Constant(k = -1.0) {
"Dyad": {"placement": {"diagram": {"x1": 10, "y1": 680, "x2": 110, "y2": 780}}}
}
"VariableLimiter with constant symmetric limits (baseline)"
vl_const = BlockComponents.Nonlinear.VariableLimiter() {
"Dyad": {"placement": {"diagram": {"x1": 260, "y1": 580, "x2": 360, "y2": 680}}}
}
relations
connect(sine.y, vl_tv.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(upperRamp.y, vl_tv.limit1) {
"Dyad": {"edges": [{"S": 1, "M": [{"x": 230, "y": 50}, {"x": 230, "y": 140}], "E": 2}]}
}
connect(lowerConst.y, vl_tv.limit2) {
"Dyad": {"edges": [{"S": 1, "M": [{"x": 230, "y": 310}, {"x": 230, "y": 200}], "E": 2}]}
}
connect(sine.y, vl_const.u) {
"Dyad": {"edges": [{"S": 1, "M": [{"x": 200, "y": 170}, {"x": 200, "y": 630}], "E": 2}]}
}
connect(upperConst.y, vl_const.limit1) {
"Dyad": {"edges": [{"S": 1, "M": [{"x": 230, "y": 500}, {"x": 230, "y": 600}], "E": 2}]}
}
connect(lowerConst2.y, vl_const.limit2) {
"Dyad": {
"edges": [
{
"S": 1,
"M": [
{"x": 110, "y": 620},
{"x": 110, "y": 730},
{"x": 215, "y": 730},
{"x": 215, "y": 660}
],
"E": 2
}
]
}
}
metadata {
"Dyad": {
"icons": {"default": "dyad://BlockComponents/Example.svg"},
"tests": {
"case1": {
"stop": 1,
"expect": {"signals": ["sine.y", "upperRamp.y", "vl_tv.y", "vl_const.y"]}
}
}
}
}
endTest Cases
julia
using BlockComponents
using DyadInterface: TransientAnalysis, rebuild_sol, ODEAlg
using ModelingToolkit: toggle_namespacing, get_initial_conditions, @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 = BlockComponents.Nonlinear.Tests.VariableLimiter()
model_case1 = toggle_namespacing(model_case1, false)
model_case1 = toggle_namespacing(model_case1, true)
result_case1 = TransientAnalysis(; model = model_case1, alg = ODEAlg.Auto(), start = 0e+0, stop = 1e+0, 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.sine.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Nonlinear.Tests.VariableLimiter_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.sine.y], width=2, label="Actual value of sine.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of sine.y")
end<< @setup-block not executed in draft mode >>julia
plt<< @example-block not executed in draft mode >>julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.upperRamp.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Nonlinear.Tests.VariableLimiter_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.upperRamp.y], width=2, label="Actual value of upperRamp.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of upperRamp.y")
end<< @setup-block not executed in draft mode >>julia
plt<< @example-block not executed in draft mode >>julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.vl_tv.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Nonlinear.Tests.VariableLimiter_case1_sig2.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.vl_tv.y], width=2, label="Actual value of vl_tv.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of vl_tv.y")
end<< @setup-block not executed in draft mode >>julia
plt<< @example-block not executed in draft mode >>julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.vl_const.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Nonlinear.Tests.VariableLimiter_case1_sig3.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.vl_const.y], width=2, label="Actual value of vl_const.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of vl_const.y")
end<< @setup-block not executed in draft mode >>julia
plt<< @example-block not executed in draft mode >>Related
Examples
Experiments
Analyses
Tests