Sources.Tests.CosineModes
Verifies all operating modes of the Cosine source against analytically known values.
Four Cosine instances share amplitude = 2, frequency = 1, start_time = 0.5, offset = 1, phase = π/4 (so 2π·frequency·start_time = π, half a cycle) and differ only in the continuous and shift_time parameters:
sig_default: new parameters left at their defaults (continuous = true,shift_time = start_time). Confirms the default reproduces the legacy value-continuous behavior (no breaking change).sig_gated:continuous = false,shift_time = start_time— output holds atoffsetbeforestart_time.sig_gated_abs:continuous = false,shift_time = 0— gated output with absolute time reference.sig_hold_abs:continuous = true,shift_time = 0— value-continuous output with absolute time reference.
Each signal feeds an integrator so the test checks both the instantaneous output (at time = 0, in the pre-start region, and at the stop time, in the active region) and the accumulated integral.
Usage
BlockComponents.Sources.Tests.CosineModes()
Behavior
using BlockComponents #hide
using ModelingToolkit #hide
@named sys = BlockComponents.Sources.Tests.CosineModes() #hide
let eqs = full_equations(sys); Base.length(eqs) > 25 ? nothing : eqs end #hide<< @example-block not executed in draft mode >>Source
"""
Verifies all operating modes of the Cosine source against analytically known values.
Four `Cosine` instances share `amplitude = 2`, `frequency = 1`, `start_time = 0.5`,
`offset = 1`, `phase = π/4` (so `2π·frequency·start_time = π`, half a cycle) and differ
only in the `continuous` and `shift_time` parameters:
- `sig_default` : new parameters left at their defaults (`continuous = true`, `shift_time = start_time`).
Confirms the default reproduces the legacy value-continuous behavior (no breaking change).
- `sig_gated` : `continuous = false`, `shift_time = start_time` — output holds at `offset` before `start_time`.
- `sig_gated_abs`: `continuous = false`, `shift_time = 0` — gated output with absolute time reference.
- `sig_hold_abs` : `continuous = true`, `shift_time = 0` — value-continuous output with absolute time reference.
Each signal feeds an integrator so the test checks both the instantaneous output
(at `time = 0`, in the pre-start region, and at the stop time, in the active region)
and the accumulated integral.
"""
test component CosineModes
sig_default = BlockComponents.Sources.Cosine(amplitude = 2, frequency = 1, start_time = 0.5, offset = 1, phase = pi / 4) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
},
"tags": []
}
}
sig_gated = BlockComponents.Sources.Cosine(amplitude = 2, frequency = 1, start_time = 0.5, offset = 1, phase = pi / 4, continuous = false) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 140, "x2": 120, "y2": 240, "rot": 0}
},
"tags": []
}
}
sig_gated_abs = BlockComponents.Sources.Cosine(amplitude = 2, frequency = 1, start_time = 0.5, offset = 1, phase = pi / 4, continuous = false, shift_time = 0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 260, "x2": 120, "y2": 360, "rot": 0}
},
"tags": []
}
}
sig_hold_abs = BlockComponents.Sources.Cosine(amplitude = 2, frequency = 1, start_time = 0.5, offset = 1, phase = pi / 4, continuous = true, shift_time = 0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 380, "x2": 120, "y2": 480, "rot": 0}
},
"tags": []
}
}
int_default = BlockComponents.Continuous.Integrator() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 160, "y1": 20, "x2": 260, "y2": 120, "rot": 0}
},
"tags": []
}
}
int_gated = BlockComponents.Continuous.Integrator() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 160, "y1": 140, "x2": 260, "y2": 240, "rot": 0}
},
"tags": []
}
}
int_gated_abs = BlockComponents.Continuous.Integrator() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 160, "y1": 260, "x2": 260, "y2": 360, "rot": 0}
},
"tags": []
}
}
int_hold_abs = BlockComponents.Continuous.Integrator() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 160, "y1": 380, "x2": 260, "y2": 480, "rot": 0}
},
"tags": []
}
}
relations
connect(sig_default.y, int_default.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(sig_gated.y, int_gated.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(sig_gated_abs.y, int_gated_abs.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(sig_hold_abs.y, int_hold_abs.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
"Dyad": {
"icons": {"default": "dyad://BlockComponents/Example.svg"},
"tests": {
"case1": {
"stop": 2,
"abstol": 1e-8,
"reltol": 1e-8,
"atol": {
"sig_default.y": 0.01,
"sig_gated.y": 0.01,
"sig_gated_abs.y": 0.01,
"sig_hold_abs.y": 0.01,
"int_default.x": 0.01,
"int_gated.x": 0.01,
"int_gated_abs.x": 0.01,
"int_hold_abs.x": 0.01
},
"expect": {
"initial": {
"sig_default.y": 2.4142,
"sig_gated.y": 1,
"sig_gated_abs.y": 1,
"sig_hold_abs.y": -0.4142
},
"signals": ["sig_default.y", "sig_gated.y", "sig_gated_abs.y", "sig_hold_abs.y"],
"final": {
"sig_default.y": -0.4142,
"sig_gated.y": -0.4142,
"sig_gated_abs.y": 2.4142,
"sig_hold_abs.y": 2.4142,
"int_default.x": 2.2569,
"int_gated.x": 1.5498,
"int_gated_abs.x": 2.4502,
"int_hold_abs.x": 1.7431
}
}
}
}
}
}
endFlattened Source
"""
Verifies all operating modes of the Cosine source against analytically known values.
Four `Cosine` instances share `amplitude = 2`, `frequency = 1`, `start_time = 0.5`,
`offset = 1`, `phase = π/4` (so `2π·frequency·start_time = π`, half a cycle) and differ
only in the `continuous` and `shift_time` parameters:
- `sig_default` : new parameters left at their defaults (`continuous = true`, `shift_time = start_time`).
Confirms the default reproduces the legacy value-continuous behavior (no breaking change).
- `sig_gated` : `continuous = false`, `shift_time = start_time` — output holds at `offset` before `start_time`.
- `sig_gated_abs`: `continuous = false`, `shift_time = 0` — gated output with absolute time reference.
- `sig_hold_abs` : `continuous = true`, `shift_time = 0` — value-continuous output with absolute time reference.
Each signal feeds an integrator so the test checks both the instantaneous output
(at `time = 0`, in the pre-start region, and at the stop time, in the active region)
and the accumulated integral.
"""
test component CosineModes
sig_default = BlockComponents.Sources.Cosine(amplitude = 2, frequency = 1, start_time = 0.5, offset = 1, phase = pi / 4) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
},
"tags": []
}
}
sig_gated = BlockComponents.Sources.Cosine(amplitude = 2, frequency = 1, start_time = 0.5, offset = 1, phase = pi / 4, continuous = false) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 140, "x2": 120, "y2": 240, "rot": 0}
},
"tags": []
}
}
sig_gated_abs = BlockComponents.Sources.Cosine(amplitude = 2, frequency = 1, start_time = 0.5, offset = 1, phase = pi / 4, continuous = false, shift_time = 0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 260, "x2": 120, "y2": 360, "rot": 0}
},
"tags": []
}
}
sig_hold_abs = BlockComponents.Sources.Cosine(amplitude = 2, frequency = 1, start_time = 0.5, offset = 1, phase = pi / 4, continuous = true, shift_time = 0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 380, "x2": 120, "y2": 480, "rot": 0}
},
"tags": []
}
}
int_default = BlockComponents.Continuous.Integrator() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 160, "y1": 20, "x2": 260, "y2": 120, "rot": 0}
},
"tags": []
}
}
int_gated = BlockComponents.Continuous.Integrator() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 160, "y1": 140, "x2": 260, "y2": 240, "rot": 0}
},
"tags": []
}
}
int_gated_abs = BlockComponents.Continuous.Integrator() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 160, "y1": 260, "x2": 260, "y2": 360, "rot": 0}
},
"tags": []
}
}
int_hold_abs = BlockComponents.Continuous.Integrator() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 160, "y1": 380, "x2": 260, "y2": 480, "rot": 0}
},
"tags": []
}
}
relations
connect(sig_default.y, int_default.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(sig_gated.y, int_gated.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(sig_gated_abs.y, int_gated_abs.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(sig_hold_abs.y, int_hold_abs.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
"Dyad": {
"icons": {"default": "dyad://BlockComponents/Example.svg"},
"tests": {
"case1": {
"stop": 2,
"abstol": 1e-8,
"reltol": 1e-8,
"atol": {
"sig_default.y": 0.01,
"sig_gated.y": 0.01,
"sig_gated_abs.y": 0.01,
"sig_hold_abs.y": 0.01,
"int_default.x": 0.01,
"int_gated.x": 0.01,
"int_gated_abs.x": 0.01,
"int_hold_abs.x": 0.01
},
"expect": {
"initial": {
"sig_default.y": 2.4142,
"sig_gated.y": 1,
"sig_gated_abs.y": 1,
"sig_hold_abs.y": -0.4142
},
"signals": ["sig_default.y", "sig_gated.y", "sig_gated_abs.y", "sig_hold_abs.y"],
"final": {
"sig_default.y": -0.4142,
"sig_gated.y": -0.4142,
"sig_gated_abs.y": 2.4142,
"sig_hold_abs.y": 2.4142,
"int_default.x": 2.2569,
"int_gated.x": 1.5498,
"int_gated_abs.x": 2.4502,
"int_hold_abs.x": 1.7431
}
}
}
}
}
}
endTest Cases
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
@named model_case1 = BlockComponents.Sources.Tests.CosineModes()
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 = 2e+0, abstol=1e-8, reltol=1e-8)
sol_case1 = rebuild_sol(result_case1)<< @setup-block not executed in draft mode >>df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.sig_default.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Sources.Tests.CosineModes_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.sig_default.y], width=2, label="Actual value of sig_default.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of sig_default.y")
end
scatter!(plt, [df_case1.t[1]], [2.4142], label="Initial Condition for `sig_default.y`")
scatter!(plt, [df_case1.t[end]], [-0.4142], label="Final Condition for `sig_default.y`")<< @setup-block not executed in draft mode >>plt<< @example-block not executed in draft mode >>df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.sig_gated.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Sources.Tests.CosineModes_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.sig_gated.y], width=2, label="Actual value of sig_gated.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of sig_gated.y")
end
scatter!(plt, [df_case1.t[1]], [1], label="Initial Condition for `sig_gated.y`")
scatter!(plt, [df_case1.t[end]], [-0.4142], label="Final Condition for `sig_gated.y`")<< @setup-block not executed in draft mode >>plt<< @example-block not executed in draft mode >>df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.sig_gated_abs.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Sources.Tests.CosineModes_case1_sig2.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.sig_gated_abs.y], width=2, label="Actual value of sig_gated_abs.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of sig_gated_abs.y")
end
scatter!(plt, [df_case1.t[1]], [1], label="Initial Condition for `sig_gated_abs.y`")
scatter!(plt, [df_case1.t[end]], [2.4142], label="Final Condition for `sig_gated_abs.y`")<< @setup-block not executed in draft mode >>plt<< @example-block not executed in draft mode >>df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.sig_hold_abs.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Sources.Tests.CosineModes_case1_sig3.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.sig_hold_abs.y], width=2, label="Actual value of sig_hold_abs.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of sig_hold_abs.y")
end
scatter!(plt, [df_case1.t[1]], [-0.4142], label="Initial Condition for `sig_hold_abs.y`")
scatter!(plt, [df_case1.t[end]], [2.4142], label="Final Condition for `sig_hold_abs.y`")<< @setup-block not executed in draft mode >>plt<< @example-block not executed in draft mode >>Related
Examples
Experiments
Analyses
Tests