DerivativeIntegratorTerminatorTest ​
Test component that demonstrates the chained behavior of differentiation and integration of a sine signal.
This component connects a sine source through a derivative block to an integrator and finally to a terminator, demonstrating that differentiating and then integrating a signal approximately recovers the original signal. The derivative block approximates the mathematical operation d/dt, while the integrator performs the operation ∫dt. When applied in sequence to a sine wave, the output should theoretically match the input with some phase shift.
Usage ​
BlockComponents.DerivativeIntegratorTerminatorTest()
Behavior ​
julia
using BlockComponents #hide
using ModelingToolkit #hide
@named sys = BlockComponents.DerivativeIntegratorTerminatorTest() #hide
full_equations(sys) #hide<< @example-block not executed in draft mode >>Source ​
dyad
"""
Test component that demonstrates the chained behavior of differentiation and integration of a sine signal.
This component connects a sine source through a derivative block to an integrator and finally to a terminator,
demonstrating that differentiating and then integrating a signal approximately recovers the original signal.
The derivative block approximates the mathematical operation d/dt, while the integrator performs the operation ∫dt.
When applied in sequence to a sine wave, the output should theoretically match the input with some phase shift.
"""
test component DerivativeIntegratorTerminatorTest
"Approximates the derivative of the input signal with configured gain and time constant"
derivative = Derivative(k = 1, T = 0.001) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 150, "y1": 20, "x2": 250, "y2": 120, "rot": 0}
}
}
}
"Integrates the input signal with specified gain"
integrator = Integrator(k = 1) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 280, "y1": 20, "x2": 380, "y2": 120, "rot": 0}
}
}
}
"Terminates the signal path, consuming the input without producing an output"
terminator = Terminator() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 410, "y1": 20, "x2": 510, "y2": 120, "rot": 0}
}
}
}
"Generates a sinusoidal signal with 1 Hz frequency and unit amplitude"
source = Sine(amplitude = 1, frequency = 1) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
}
}
}
relations
"Connects the sine wave output to the derivative block input"
connect(source.y, derivative.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
"Connects the derivative output to the integrator input"
connect(derivative.y, integrator.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
"Connects the integrator output to the terminator input"
connect(integrator.y, terminator.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
"Dyad": {
"icons": {"default": "dyad://BlockComponents/Example.svg"},
"experiments": {},
"tests": {
"case1": {
"stop": 10,
"atol": {"integrator.y": 0.01, "terminator.u": 0.01},
"expect": {
"final": {"integrator.y": 0, "terminator.u": 0},
"signals": ["integrator.y", "terminator.u"]
}
}
}
}
}
endFlattened Source
dyad
"""
Test component that demonstrates the chained behavior of differentiation and integration of a sine signal.
This component connects a sine source through a derivative block to an integrator and finally to a terminator,
demonstrating that differentiating and then integrating a signal approximately recovers the original signal.
The derivative block approximates the mathematical operation d/dt, while the integrator performs the operation ∫dt.
When applied in sequence to a sine wave, the output should theoretically match the input with some phase shift.
"""
test component DerivativeIntegratorTerminatorTest
"Approximates the derivative of the input signal with configured gain and time constant"
derivative = Derivative(k = 1, T = 0.001) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 150, "y1": 20, "x2": 250, "y2": 120, "rot": 0}
}
}
}
"Integrates the input signal with specified gain"
integrator = Integrator(k = 1) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 280, "y1": 20, "x2": 380, "y2": 120, "rot": 0}
}
}
}
"Terminates the signal path, consuming the input without producing an output"
terminator = Terminator() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 410, "y1": 20, "x2": 510, "y2": 120, "rot": 0}
}
}
}
"Generates a sinusoidal signal with 1 Hz frequency and unit amplitude"
source = Sine(amplitude = 1, frequency = 1) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
}
}
}
relations
"Connects the sine wave output to the derivative block input"
connect(source.y, derivative.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
"Connects the derivative output to the integrator input"
connect(derivative.y, integrator.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
"Connects the integrator output to the terminator input"
connect(integrator.y, terminator.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
"Dyad": {
"icons": {"default": "dyad://BlockComponents/Example.svg"},
"experiments": {},
"tests": {
"case1": {
"stop": 10,
"atol": {"integrator.y": 0.01, "terminator.u": 0.01},
"expect": {
"final": {"integrator.y": 0, "terminator.u": 0},
"signals": ["integrator.y", "terminator.u"]
}
}
}
}
}
endTest Cases ​
julia
using BlockComponents
using DyadInterface: TransientAnalysis, rebuild_sol
using ModelingToolkit: toggle_namespacing, get_defaults, @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 = DerivativeIntegratorTerminatorTest()
model_case1 = toggle_namespacing(model_case1, false)
model_case1 = toggle_namespacing(model_case1, true)
result_case1 = TransientAnalysis(; model = model_case1, alg = "auto", start = 0e+0, stop = 1e+1, 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.integrator.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "DerivativeIntegratorTerminatorTest_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.integrator.y], width=2, label="Actual value of integrator.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of integrator.y")
end
scatter!(plt, [df_case1.t[end]], [0], label="Final Condition for `integrator.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.terminator.u])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "DerivativeIntegratorTerminatorTest_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.terminator.u], width=2, label="Actual value of terminator.u")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of terminator.u")
end
scatter!(plt, [df_case1.t[end]], [0], label="Final Condition for `terminator.u`")<< @setup-block not executed in draft mode >>julia
plt<< @example-block not executed in draft mode >>Related ​
Examples
Experiments
Analyses
Tests