LIBRARY
Sources.Tests.BooleanConstant
Test that validates the BooleanConstant source for both output values.
Two constant Boolean sources — k = true and k = false — are each converted to Real via BooleanToReal so the constant outputs can be snapshotted as trajectories. Expected: toReal_true.y = 1.0 and toReal_false.y = 0.0 for the whole simulation.
Usage
BlockComponents.Sources.Tests.BooleanConstant()
Behavior
julia
using BlockComponents #hide
using ModelingToolkit #hide
@named sys = BlockComponents.Sources.Tests.BooleanConstant() #hide
let eqs = full_equations(sys); Base.length(eqs) > 25 ? nothing : eqs end #hide<< @example-block not executed in draft mode >>Source
dyad
"""
Test that validates the BooleanConstant source for both output values.
Two constant Boolean sources — `k = true` and `k = false` — are each converted to
Real via `BooleanToReal` so the constant outputs can be snapshotted as
trajectories. Expected: `toReal_true.y = 1.0` and `toReal_false.y = 0.0` for the
whole simulation.
"""
test component BooleanConstant
"Constant Boolean source (k = true)"
source_true = BlockComponents.Sources.BooleanConstant(k = true) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
}
}
}
"Converts the true Boolean output to Real"
toReal_true = BlockComponents.Math.BooleanToReal() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 300, "y1": 20, "x2": 400, "y2": 120, "rot": 0}
}
}
}
"Constant Boolean source (k = false)"
source_false = BlockComponents.Sources.BooleanConstant(k = false) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 220, "x2": 120, "y2": 320, "rot": 0}
}
}
}
"Converts the false Boolean output to Real"
toReal_false = BlockComponents.Math.BooleanToReal() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 300, "y1": 220, "x2": 400, "y2": 320, "rot": 0}
}
}
}
relations
connect(source_true.y, toReal_true.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(source_false.y, toReal_false.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
"Dyad": {
"icons": {"default": "dyad://BlockComponents/Example.svg"},
"tests": {
"case1": {"stop": 1, "expect": {"signals": ["toReal_true.y", "toReal_false.y"]}}
}
}
}
endFlattened Source
dyad
"""
Test that validates the BooleanConstant source for both output values.
Two constant Boolean sources — `k = true` and `k = false` — are each converted to
Real via `BooleanToReal` so the constant outputs can be snapshotted as
trajectories. Expected: `toReal_true.y = 1.0` and `toReal_false.y = 0.0` for the
whole simulation.
"""
test component BooleanConstant
"Constant Boolean source (k = true)"
source_true = BlockComponents.Sources.BooleanConstant(k = true) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
}
}
}
"Converts the true Boolean output to Real"
toReal_true = BlockComponents.Math.BooleanToReal() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 300, "y1": 20, "x2": 400, "y2": 120, "rot": 0}
}
}
}
"Constant Boolean source (k = false)"
source_false = BlockComponents.Sources.BooleanConstant(k = false) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 220, "x2": 120, "y2": 320, "rot": 0}
}
}
}
"Converts the false Boolean output to Real"
toReal_false = BlockComponents.Math.BooleanToReal() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 300, "y1": 220, "x2": 400, "y2": 320, "rot": 0}
}
}
}
relations
connect(source_true.y, toReal_true.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(source_false.y, toReal_false.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
"Dyad": {
"icons": {"default": "dyad://BlockComponents/Example.svg"},
"tests": {
"case1": {"stop": 1, "expect": {"signals": ["toReal_true.y", "toReal_false.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.Sources.Tests.BooleanConstant()
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.toReal_true.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Sources.Tests.BooleanConstant_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.toReal_true.y], width=2, label="Actual value of toReal_true.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of toReal_true.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.toReal_false.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Sources.Tests.BooleanConstant_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.toReal_false.y], width=2, label="Actual value of toReal_false.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of toReal_false.y")
end<< @setup-block not executed in draft mode >>julia
plt<< @example-block not executed in draft mode >>Related
Examples
Experiments
Analyses
Tests