Skip to content
DerivativeIntegratorTerminatorTest.md

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

source.y(t)=derivative.u(t)derivative.y(t)=integrator.u(t)integrator.y(t)=terminator.u(t)dderivative.x(t)dt=derivative.u(t)derivative.x(t)derivative.Tderivative.y(t)=derivative.k(derivative.u(t)derivative.x(t))derivative.Tdintegrator.x(t)dt=integrator.kintegrator.u(t)integrator.y(t)=integrator.x(t)source.y(t)=ifelse(source.start_time<t,source.offset+source.amplitudesin(source.phase+6.2832source.frequency(source.start_time+t)),source.offset)

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

This is setup code, that must be run before each test case.

julia
using BlockComponents
using ModelingToolkit, OrdinaryDiffEqDefault
using Plots
using CSV, DataFrames

snapshotsdir = joinpath(dirname(dirname(pathof(BlockComponents))), "test", "snapshots")
"/home/actions-runner-10/.julia/packages/BlockComponents/77kIK/test/snapshots"

Test Case case1

julia
@mtkbuild model_case1 = DerivativeIntegratorTerminatorTest()
u0_case1 = []
prob_case1 = ODEProblem(model_case1, u0_case1, (0, 10))
sol_case1 = solve(prob_case1)
retcode: Success
Interpolation: 3rd order Hermite
t: 3178-element Vector{Float64}:
  0.0
  9.999999999999999e-5
  0.00028789730152180846
  0.0005230564104651719
  0.0008296770746536177
  0.001211489191159881
  0.0016886376573750862
  0.0022747151649437475
  0.0029943999452585407
  0.003876098766736835

  9.973922966073847
  9.976609681661179
  9.979465321027716
  9.982794738925772
  9.986269480456839
  9.989835080589735
  9.99337536360005
  9.99681732395307
 10.0
u: 3178-element Vector{Vector{Float64}}:
 [0.0, 0.0]
 [3.039439331034553e-5, 3.039439331034553e-5]
 [0.0002371015873213095, 0.0002371015873213095]
 [0.0007273574664967518, 0.0007273574664967518]
 [0.0016704890478819816, 0.0016704890478819816]
 [0.0031996498734421365, 0.0031996498734421365]
 [0.0054877305487789916, 0.0054877305487789916]
 [0.008655170260897386, 0.008655170260897386]
 [0.012845277603621747, 0.012845277603621747]
 [0.01820009692819799, 0.01820009692819799]

 [-0.1693061351334995, -0.1693061351334995]
 [-0.15264554299720412, -0.15264554299720412]
 [-0.13488959059144862, -0.13488959059144862]
 [-0.11413116986492222, -0.11413116986492222]
 [-0.09241305201095988, -0.09241305201095988]
 [-0.07008046020112206, -0.07008046020112206]
 [-0.0478726286513241, -0.0478726286513241]
 [-0.02626213603235824, -0.02626213603235824]
 [-0.006274336615756881, -0.006274336615756881]
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`")

plt

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`")

plt