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
DerivativeIntegratorTerminatorTest()
Behavior
julia
using BlockComponents #hide
using ModelingToolkit #hide
@named sys = 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)
# Integrates the input signal with specified gain
integrator = Integrator(k=1)
# Terminates the signal path, consuming the input without producing an output
terminator = Terminator()
# Generates a sinusoidal signal with 1 Hz frequency and unit amplitude
source = Sine(amplitude=1, frequency=1)
relations
# Connects the sine wave output to the derivative block input
connect(source.y, derivative.u)
# Connects the derivative output to the integrator input
connect(derivative.y, integrator.u)
# Connects the integrator output to the terminator input
connect(integrator.y, terminator.u)
metadata {
"Dyad": {
"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"]
}
}
}
}
}
end
Flattened 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)
# Integrates the input signal with specified gain
integrator = Integrator(k=1)
# Terminates the signal path, consuming the input without producing an output
terminator = Terminator()
# Generates a sinusoidal signal with 1 Hz frequency and unit amplitude
source = Sine(amplitude=1, frequency=1)
relations
# Connects the sine wave output to the derivative block input
connect(source.y, derivative.u)
# Connects the derivative output to the integrator input
connect(derivative.y, integrator.u)
# Connects the integrator output to the terminator input
connect(integrator.y, terminator.u)
metadata {
"Dyad": {
"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"]
}
}
}
}
}
end
Test Cases
julia
using BlockComponents
using ModelingToolkit, OrdinaryDiffEqDefault
using Plots
using CSV, DataFrames
snapshotsdir = joinpath(dirname(dirname(pathof(BlockComponents))), "test", "snapshots")
<< @setup-block not executed in draft mode >>
Test Case case1
julia
@mtkbuild model_case1 = DerivativeIntegratorTerminatorTest()
u0_case1 = []
prob_case1 = ODEProblem(model_case1, u0_case1, (0, 10))
sol_case1 = solve(prob_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