Skip to content
ContinuousClockTest.md

ContinuousClockTest

Test that evaluates a continuous clock signal integrated over time.

This test component connects a continuous clock signal to an integrator to validate the clock's behavior. The continuous clock starts at time 0.5 with an offset of 1.0, generating a signal that increases linearly with time (y = t + offset when t ≥ start_time). The integrator accumulates this signal, resulting in a quadratic response. The test verifies both the clock output and the integrated value against expected results at t=5.

Usage

ContinuousClockTest()

Behavior

signal.y(t)=integrator.u(t)dintegrator.x(t)dt=integrator.kintegrator.u(t)integrator.y(t)=integrator.x(t)signal.y(t)=ifelse(signal.start_time<t,signal.offsetsignal.start_time+t,signal.offset)

Source

dyad
# Test that evaluates a continuous clock signal integrated over time.
#
# This test component connects a continuous clock signal to an integrator to validate the
# clock's behavior. The continuous clock starts at time 0.5 with an offset of 1.0, generating
# a signal that increases linearly with time (y = t + offset when t ≥ start_time). The integrator
# accumulates this signal, resulting in a quadratic response. The test verifies both the clock
# output and the integrated value against expected results at t=5.
test component ContinuousClockTest
  # Integrator that accumulates the clock signal
  integrator = Integrator()
  # Continuous clock with 0.5 start time and 1.0 offset
  signal = ContinuousClock(start_time=0.5, offset=1.0)
relations
  # Connects the clock output to the integrator input
  connect(signal.y, integrator.u)
metadata {
  "Dyad": {
    "tests": {
      "case1": {
        "stop": 5,
        "atol": {"integrator.x": 0.001},
        "expect": {
          "initial": {"signal.y": 1},
          "signals": ["signal.y", "integrator.x"],
          "final": {"signal.y": 5.5, "integrator.x": 15.11887}
        }
      }
    }
  }
}
end
Flattened Source
dyad
# Test that evaluates a continuous clock signal integrated over time.
#
# This test component connects a continuous clock signal to an integrator to validate the
# clock's behavior. The continuous clock starts at time 0.5 with an offset of 1.0, generating
# a signal that increases linearly with time (y = t + offset when t ≥ start_time). The integrator
# accumulates this signal, resulting in a quadratic response. The test verifies both the clock
# output and the integrated value against expected results at t=5.
test component ContinuousClockTest
  # Integrator that accumulates the clock signal
  integrator = Integrator()
  # Continuous clock with 0.5 start time and 1.0 offset
  signal = ContinuousClock(start_time=0.5, offset=1.0)
relations
  # Connects the clock output to the integrator input
  connect(signal.y, integrator.u)
metadata {
  "Dyad": {
    "tests": {
      "case1": {
        "stop": 5,
        "atol": {"integrator.x": 0.001},
        "expect": {
          "initial": {"signal.y": 1},
          "signals": ["signal.y", "integrator.x"],
          "final": {"signal.y": 5.5, "integrator.x": 15.11887}
        }
      }
    }
  }
}
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 = ContinuousClockTest()
u0_case1 = []
prob_case1 = ODEProblem(model_case1, u0_case1, (0, 5))
sol_case1 = solve(prob_case1)
retcode: Success
Interpolation: 3rd order Hermite
t: 8-element Vector{Float64}:
 0.0
 9.999999999999999e-5
 0.0010999999999999998
 0.011099999999999997
 0.11109999999999996
 1.1110999999999995
 1.62046524772359
 5.0
u: 8-element Vector{Vector{Float64}}:
 [0.0]
 [9.999999999999996e-5]
 [0.0010999999999999996]
 [0.011099999999999994]
 [0.11109999999999993]
 [1.2916977615762024]
 [2.2420625899779356]
 [15.1188761565762]
julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.signal.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "ContinuousClockTest_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.signal.y], width=2, label="Actual value of signal.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of signal.y")
end
scatter!(plt, [df_case1.t[1]], [1], label="Initial Condition for `signal.y`")
scatter!(plt, [df_case1.t[end]], [5.5], label="Final Condition for `signal.y`")

plt

julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.integrator.x])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "ContinuousClockTest_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.integrator.x], width=2, label="Actual value of integrator.x")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of integrator.x")
end
scatter!(plt, [df_case1.t[end]], [15.11887], label="Final Condition for `integrator.x`")

plt