LIBRARY
Sources.Tests.Chirp
Test component that verifies linear, quadratic, and exponential chirp signal generators.
This component instantiates three Chirp blocks — one with linear, one with quadratic, and one with exponential frequency sweep law — and connects each to an Integrator to validate the time-domain integration of the chirp signals. With default parameters (fmin=0.1 Hz, fmax=5 Hz, T=100 s, amplitude=1), the linear chirp sweeps frequency uniformly while the quadratic chirp sweeps with a slow start and fast rise.
Usage
BlockComponents.Sources.Tests.Chirp()
Behavior
julia
using BlockComponents #hide
using ModelingToolkit #hide
@named sys = BlockComponents.Sources.Tests.Chirp() #hide
full_equations(sys) #hide<< @example-block not executed in draft mode >>Source
dyad
"""
Test component that verifies linear, quadratic, and exponential chirp signal generators.
This component instantiates three Chirp blocks — one with linear, one with quadratic,
and one with exponential frequency sweep law — and connects each to an Integrator to validate the time-domain integration
of the chirp signals. With default parameters (fmin=0.1 Hz, fmax=5 Hz, T=100 s, amplitude=1),
the linear chirp sweeps frequency uniformly while the quadratic chirp sweeps with a slow start
and fast rise.
"""
test component Chirp
"Integrator for the linear chirp signal"
linear_integrator = BlockComponents.Continuous.Integrator() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 200, "y1": 20, "x2": 300, "y2": 120, "rot": 0}
}
}
}
"Integrator for the quadratic chirp signal"
quadratic_integrator = BlockComponents.Continuous.Integrator() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 200, "y1": 170, "x2": 300, "y2": 270, "rot": 0}
}
}
}
"Linear chirp signal generator"
linear = BlockComponents.Sources.Chirp(law = BlockComponents.Sources.ChirpLaw.Linear()) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
},
"tags": []
}
}
"Quadratic chirp signal generator"
quadratic = BlockComponents.Sources.Chirp() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 170, "x2": 120, "y2": 270, "rot": 0}
},
"tags": []
}
}
"Integrator for the exponential chirp signal"
exponential_integrator = BlockComponents.Continuous.Integrator() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 200, "y1": 320, "x2": 300, "y2": 420, "rot": 0}
}
}
}
"Exponential chirp signal generator"
exponential = BlockComponents.Sources.Chirp(law = BlockComponents.Sources.ChirpLaw.Exponential()) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 320, "x2": 120, "y2": 420, "rot": 0}
},
"tags": []
}
}
relations
"Connects the linear chirp output to its integrator"
connect(linear.y, linear_integrator.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
"Connects the quadratic chirp output to its integrator"
connect(quadratic.y, quadratic_integrator.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
"Connects the exponential chirp output to its integrator"
connect(exponential.y, exponential_integrator.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
"Dyad": {
"icons": {"default": "dyad://BlockComponents/Example.svg"},
"tests": {
"case1": {
"stop": 100,
"atol": {
"linear.y": 0.001,
"quadratic.y": 0.001,
"exponential.y": 0.001,
"linear_integrator.x": 0.01,
"quadratic_integrator.x": 0.01,
"exponential_integrator.x": 0.01
},
"expect": {
"initial": {"linear.y": 0, "quadratic.y": 0, "exponential.y": 0},
"signals": [
"linear.y",
"quadratic.y",
"exponential.y",
"linear_integrator.x",
"quadratic_integrator.x",
"exponential_integrator.x"
],
"final": {
"linear.y": 0,
"quadratic.y": 0.866,
"exponential.y": 0.9995,
"linear_integrator.x": 1.12,
"quadratic_integrator.x": 1.66,
"exponential_integrator.x": 1.58
}
}
}
}
}
}
endFlattened Source
dyad
"""
Test component that verifies linear, quadratic, and exponential chirp signal generators.
This component instantiates three Chirp blocks — one with linear, one with quadratic,
and one with exponential frequency sweep law — and connects each to an Integrator to validate the time-domain integration
of the chirp signals. With default parameters (fmin=0.1 Hz, fmax=5 Hz, T=100 s, amplitude=1),
the linear chirp sweeps frequency uniformly while the quadratic chirp sweeps with a slow start
and fast rise.
"""
test component Chirp
"Integrator for the linear chirp signal"
linear_integrator = BlockComponents.Continuous.Integrator() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 200, "y1": 20, "x2": 300, "y2": 120, "rot": 0}
}
}
}
"Integrator for the quadratic chirp signal"
quadratic_integrator = BlockComponents.Continuous.Integrator() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 200, "y1": 170, "x2": 300, "y2": 270, "rot": 0}
}
}
}
"Linear chirp signal generator"
linear = BlockComponents.Sources.Chirp(law = BlockComponents.Sources.ChirpLaw.Linear()) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
},
"tags": []
}
}
"Quadratic chirp signal generator"
quadratic = BlockComponents.Sources.Chirp() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 170, "x2": 120, "y2": 270, "rot": 0}
},
"tags": []
}
}
"Integrator for the exponential chirp signal"
exponential_integrator = BlockComponents.Continuous.Integrator() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 200, "y1": 320, "x2": 300, "y2": 420, "rot": 0}
}
}
}
"Exponential chirp signal generator"
exponential = BlockComponents.Sources.Chirp(law = BlockComponents.Sources.ChirpLaw.Exponential()) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 320, "x2": 120, "y2": 420, "rot": 0}
},
"tags": []
}
}
relations
"Connects the linear chirp output to its integrator"
connect(linear.y, linear_integrator.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
"Connects the quadratic chirp output to its integrator"
connect(quadratic.y, quadratic_integrator.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
"Connects the exponential chirp output to its integrator"
connect(exponential.y, exponential_integrator.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
"Dyad": {
"icons": {"default": "dyad://BlockComponents/Example.svg"},
"tests": {
"case1": {
"stop": 100,
"atol": {
"linear.y": 0.001,
"quadratic.y": 0.001,
"exponential.y": 0.001,
"linear_integrator.x": 0.01,
"quadratic_integrator.x": 0.01,
"exponential_integrator.x": 0.01
},
"expect": {
"initial": {"linear.y": 0, "quadratic.y": 0, "exponential.y": 0},
"signals": [
"linear.y",
"quadratic.y",
"exponential.y",
"linear_integrator.x",
"quadratic_integrator.x",
"exponential_integrator.x"
],
"final": {
"linear.y": 0,
"quadratic.y": 0.866,
"exponential.y": 0.9995,
"linear_integrator.x": 1.12,
"quadratic_integrator.x": 1.66,
"exponential_integrator.x": 1.58
}
}
}
}
}
}
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.Chirp()
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+2, 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.linear.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Sources.Tests.Chirp_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.linear.y], width=2, label="Actual value of linear.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of linear.y")
end
scatter!(plt, [df_case1.t[1]], [0], label="Initial Condition for `linear.y`")
scatter!(plt, [df_case1.t[end]], [0], label="Final Condition for `linear.y`")<< @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.quadratic.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Sources.Tests.Chirp_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.quadratic.y], width=2, label="Actual value of quadratic.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of quadratic.y")
end
scatter!(plt, [df_case1.t[1]], [0], label="Initial Condition for `quadratic.y`")
scatter!(plt, [df_case1.t[end]], [0.866], label="Final Condition for `quadratic.y`")<< @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.exponential.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Sources.Tests.Chirp_case1_sig2.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.exponential.y], width=2, label="Actual value of exponential.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of exponential.y")
end
scatter!(plt, [df_case1.t[1]], [0], label="Initial Condition for `exponential.y`")
scatter!(plt, [df_case1.t[end]], [0.9995], label="Final Condition for `exponential.y`")<< @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.linear_integrator.x])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Sources.Tests.Chirp_case1_sig3.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.linear_integrator.x], width=2, label="Actual value of linear_integrator.x")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of linear_integrator.x")
end
scatter!(plt, [df_case1.t[end]], [1.12], label="Final Condition for `linear_integrator.x`")<< @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.quadratic_integrator.x])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Sources.Tests.Chirp_case1_sig4.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.quadratic_integrator.x], width=2, label="Actual value of quadratic_integrator.x")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of quadratic_integrator.x")
end
scatter!(plt, [df_case1.t[end]], [1.66], label="Final Condition for `quadratic_integrator.x`")<< @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.exponential_integrator.x])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Sources.Tests.Chirp_case1_sig5.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.exponential_integrator.x], width=2, label="Actual value of exponential_integrator.x")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of exponential_integrator.x")
end
scatter!(plt, [df_case1.t[end]], [1.58], label="Final Condition for `exponential_integrator.x`")<< @setup-block not executed in draft mode >>julia
plt<< @example-block not executed in draft mode >>Related
Examples
Experiments
Analyses
Tests