LIBRARY
Logical.Tests.ZeroThresholds
MSL cross-validation: ModelicaTest.Blocks.ZeroThresholds
Mirrors the MSL test exactly. A ramp (1 → 0 over 0.2 s starting at t=0.3) feeds through two limiter paths:
Positive path: Limiter(y_min=0) → GreaterThreshold(0), GreaterEqualThreshold(0)
Negative path: Gain(k=-1) → Limiter(y_max=0) → LessThreshold(0), LessEqualThreshold(0)
Verifies that threshold comparisons at zero behave correctly when the input signal is clamped to exactly zero by the limiter.
Usage
BlockComponents.Logical.Tests.ZeroThresholds()
Behavior
julia
using BlockComponents #hide
using ModelingToolkit #hide
@named sys = BlockComponents.Logical.Tests.ZeroThresholds() #hide
let eqs = full_equations(sys); Base.length(eqs) > 25 ? nothing : eqs end #hide<< @example-block not executed in draft mode >>Source
dyad
"""
MSL cross-validation: ModelicaTest.Blocks.ZeroThresholds
Mirrors the MSL test exactly. A ramp (1 → 0 over 0.2 s starting at t=0.3)
feeds through two limiter paths:
- Positive path: Limiter(y_min=0) → GreaterThreshold(0), GreaterEqualThreshold(0)
- Negative path: Gain(k=-1) → Limiter(y_max=0) → LessThreshold(0), LessEqualThreshold(0)
Verifies that threshold comparisons at zero behave correctly when the input
signal is clamped to exactly zero by the limiter.
"""
test component ZeroThresholds
ramp = BlockComponents.Sources.Ramp(duration = 0.2, height = -1.0, offset = 1.0, start_time = 0.3) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
},
"tags": []
}
}
gain = BlockComponents.Math.Gain(k = -1) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 160, "y1": 20, "x2": 260, "y2": 120, "rot": 0}
},
"tags": []
}
}
limiter_min_zero = BlockComponents.Nonlinear.Limiter(y_max = 1e5, y_min = 0.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 160, "y1": 140, "x2": 260, "y2": 240, "rot": 0}
},
"tags": []
}
}
limiter_max_zero = BlockComponents.Nonlinear.Limiter(y_max = 0.0, y_min = -1e5) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 300.5, "y1": 20, "x2": 400.5, "y2": 120, "rot": 0}
},
"tags": []
}
}
greater_threshold = BlockComponents.Logical.GreaterThreshold(threshold = 0.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 301, "y1": 140, "x2": 401, "y2": 240, "rot": 0}
},
"tags": []
}
}
greater_equal_threshold = BlockComponents.Logical.GreaterEqualThreshold(threshold = 0.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 301, "y1": 260, "x2": 401, "y2": 360, "rot": 0}
},
"tags": []
}
}
less_threshold = BlockComponents.Logical.LessThreshold(threshold = 0.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 442, "y1": 140, "x2": 542, "y2": 240, "rot": 0}
},
"tags": []
}
}
less_equal_threshold = BlockComponents.Logical.LessEqualThreshold(threshold = 0.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 450, "y1": 20, "x2": 550, "y2": 120, "rot": 0}
},
"tags": []
}
}
relations
connect(ramp.y, limiter_min_zero.u) {
"Dyad": {
"edges": [{"S": 1, "M": [], "E": -1}, {"S": -1, "M": [{"x": 140, "y": 190}], "E": 2}],
"junctions": [{"x": 140, "y": 70}],
"renderStyle": "standard"
}
}
connect(ramp.y, gain.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(gain.y, limiter_max_zero.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(limiter_min_zero.y, greater_threshold.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(limiter_min_zero.y, greater_equal_threshold.u) {
"Dyad": {
"edges": [{"S": 1, "M": [], "E": -1}, {"S": -1, "M": [{"x": 280, "y": 310}], "E": 2}],
"junctions": [{"x": 280, "y": 190}],
"renderStyle": "standard"
}
}
connect(limiter_max_zero.y, less_threshold.u) {
"Dyad": {
"edges": [{"S": 1, "M": [], "E": -1}, {"S": -1, "M": [{"x": 421, "y": 190}], "E": 2}],
"junctions": [{"x": 421, "y": 70}],
"renderStyle": "standard"
}
}
connect(limiter_max_zero.y, less_equal_threshold.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
"Dyad": {
"icons": {"default": "dyad://BlockComponents/Example.svg"},
"tests": {
"case1": {
"stop": 1,
"expect": {
"signals": [
"ramp.y",
"limiter_min_zero.y",
"limiter_max_zero.y",
"greater_threshold.y",
"greater_equal_threshold.y",
"less_threshold.y",
"less_equal_threshold.y"
]
}
}
}
}
}
endFlattened Source
dyad
"""
MSL cross-validation: ModelicaTest.Blocks.ZeroThresholds
Mirrors the MSL test exactly. A ramp (1 → 0 over 0.2 s starting at t=0.3)
feeds through two limiter paths:
- Positive path: Limiter(y_min=0) → GreaterThreshold(0), GreaterEqualThreshold(0)
- Negative path: Gain(k=-1) → Limiter(y_max=0) → LessThreshold(0), LessEqualThreshold(0)
Verifies that threshold comparisons at zero behave correctly when the input
signal is clamped to exactly zero by the limiter.
"""
test component ZeroThresholds
ramp = BlockComponents.Sources.Ramp(duration = 0.2, height = -1.0, offset = 1.0, start_time = 0.3) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
},
"tags": []
}
}
gain = BlockComponents.Math.Gain(k = -1) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 160, "y1": 20, "x2": 260, "y2": 120, "rot": 0}
},
"tags": []
}
}
limiter_min_zero = BlockComponents.Nonlinear.Limiter(y_max = 1e5, y_min = 0.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 160, "y1": 140, "x2": 260, "y2": 240, "rot": 0}
},
"tags": []
}
}
limiter_max_zero = BlockComponents.Nonlinear.Limiter(y_max = 0.0, y_min = -1e5) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 300.5, "y1": 20, "x2": 400.5, "y2": 120, "rot": 0}
},
"tags": []
}
}
greater_threshold = BlockComponents.Logical.GreaterThreshold(threshold = 0.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 301, "y1": 140, "x2": 401, "y2": 240, "rot": 0}
},
"tags": []
}
}
greater_equal_threshold = BlockComponents.Logical.GreaterEqualThreshold(threshold = 0.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 301, "y1": 260, "x2": 401, "y2": 360, "rot": 0}
},
"tags": []
}
}
less_threshold = BlockComponents.Logical.LessThreshold(threshold = 0.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 442, "y1": 140, "x2": 542, "y2": 240, "rot": 0}
},
"tags": []
}
}
less_equal_threshold = BlockComponents.Logical.LessEqualThreshold(threshold = 0.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 450, "y1": 20, "x2": 550, "y2": 120, "rot": 0}
},
"tags": []
}
}
relations
connect(ramp.y, limiter_min_zero.u) {
"Dyad": {
"edges": [{"S": 1, "M": [], "E": -1}, {"S": -1, "M": [{"x": 140, "y": 190}], "E": 2}],
"junctions": [{"x": 140, "y": 70}],
"renderStyle": "standard"
}
}
connect(ramp.y, gain.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(gain.y, limiter_max_zero.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(limiter_min_zero.y, greater_threshold.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(limiter_min_zero.y, greater_equal_threshold.u) {
"Dyad": {
"edges": [{"S": 1, "M": [], "E": -1}, {"S": -1, "M": [{"x": 280, "y": 310}], "E": 2}],
"junctions": [{"x": 280, "y": 190}],
"renderStyle": "standard"
}
}
connect(limiter_max_zero.y, less_threshold.u) {
"Dyad": {
"edges": [{"S": 1, "M": [], "E": -1}, {"S": -1, "M": [{"x": 421, "y": 190}], "E": 2}],
"junctions": [{"x": 421, "y": 70}],
"renderStyle": "standard"
}
}
connect(limiter_max_zero.y, less_equal_threshold.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
"Dyad": {
"icons": {"default": "dyad://BlockComponents/Example.svg"},
"tests": {
"case1": {
"stop": 1,
"expect": {
"signals": [
"ramp.y",
"limiter_min_zero.y",
"limiter_max_zero.y",
"greater_threshold.y",
"greater_equal_threshold.y",
"less_threshold.y",
"less_equal_threshold.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.Logical.Tests.ZeroThresholds()
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.ramp.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Logical.Tests.ZeroThresholds_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.ramp.y], width=2, label="Actual value of ramp.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of ramp.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.limiter_min_zero.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Logical.Tests.ZeroThresholds_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.limiter_min_zero.y], width=2, label="Actual value of limiter_min_zero.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of limiter_min_zero.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.limiter_max_zero.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Logical.Tests.ZeroThresholds_case1_sig2.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.limiter_max_zero.y], width=2, label="Actual value of limiter_max_zero.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of limiter_max_zero.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.greater_threshold.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Logical.Tests.ZeroThresholds_case1_sig3.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.greater_threshold.y], width=2, label="Actual value of greater_threshold.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of greater_threshold.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.greater_equal_threshold.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Logical.Tests.ZeroThresholds_case1_sig4.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.greater_equal_threshold.y], width=2, label="Actual value of greater_equal_threshold.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of greater_equal_threshold.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.less_threshold.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Logical.Tests.ZeroThresholds_case1_sig5.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.less_threshold.y], width=2, label="Actual value of less_threshold.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of less_threshold.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.less_equal_threshold.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Logical.Tests.ZeroThresholds_case1_sig6.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.less_equal_threshold.y], width=2, label="Actual value of less_equal_threshold.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of less_equal_threshold.y")
end<< @setup-block not executed in draft mode >>julia
plt<< @example-block not executed in draft mode >>Related
Examples
Experiments
Analyses
Tests