Skip to content
CosineTest.md

CosineTest

Tests the integration of a cosine signal with configurable parameters.

This component connects a cosine signal generator to an integrator and verifies the correct integration behavior through test cases. The cosine signal is defined by:

y=offset+amplitudecos(2πfrequency(timestart_time)+phase)

The integration of this signal is verified against expected values at specific time points.

Usage

CosineTest()

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.offset+signal.amplitudecos(signal.phase+6.2832signal.frequency(signal.start_time+t)),signal.offset)

Source

dyad
# Tests the integration of a cosine signal with configurable parameters.
#
# This component connects a cosine signal generator to an integrator and verifies the correct
# integration behavior through test cases. The cosine signal is defined by:
#
# ```math
# y = offset + amplitude \cdot \cos(2\pi \cdot frequency \cdot (time - start\_time) + phase)
# ```
#
# The integration of this signal is verified against expected values at specific time points.
test component CosineTest
  # Integrator component that accumulates the input signal
  integrator = Integrator()
  # Cosine signal generator with specified amplitude, frequency, start time, offset, and phase
  signal = Cosine(amplitude=1, frequency=2, start_time=0.5, offset=0.5, phase=3.14)
relations
  # Connects the output of the cosine signal to the input of the integrator
  connect(signal.y, integrator.u)
metadata {
  "Dyad": {
    "tests": {
      "case1": {
        "stop": 5,
        "atol": {"signal.y": 0.001, "integrator.x": 0.001},
        "expect": {
          "initial": {"signal.y": 0.5},
          "signals": ["signal.y", "integrator.x"],
          "final": {"signal.y": -0.49999, "integrator.x": 2.45255}
        }
      }
    }
  }
}
end
Flattened Source
dyad
# Tests the integration of a cosine signal with configurable parameters.
#
# This component connects a cosine signal generator to an integrator and verifies the correct
# integration behavior through test cases. The cosine signal is defined by:
#
# ```math
# y = offset + amplitude \cdot \cos(2\pi \cdot frequency \cdot (time - start\_time) + phase)
# ```
#
# The integration of this signal is verified against expected values at specific time points.
test component CosineTest
  # Integrator component that accumulates the input signal
  integrator = Integrator()
  # Cosine signal generator with specified amplitude, frequency, start time, offset, and phase
  signal = Cosine(amplitude=1, frequency=2, start_time=0.5, offset=0.5, phase=3.14)
relations
  # Connects the output of the cosine signal to the input of the integrator
  connect(signal.y, integrator.u)
metadata {
  "Dyad": {
    "tests": {
      "case1": {
        "stop": 5,
        "atol": {"signal.y": 0.001, "integrator.x": 0.001},
        "expect": {
          "initial": {"signal.y": 0.5},
          "signals": ["signal.y", "integrator.x"],
          "final": {"signal.y": -0.49999, "integrator.x": 2.45255}
        }
      }
    }
  }
}
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 = CosineTest()
u0_case1 = []
prob_case1 = ODEProblem(model_case1, u0_case1, (0, 5))
sol_case1 = solve(prob_case1)
retcode: Success
Interpolation: 3rd order Hermite
t: 27-element Vector{Float64}:
 0.0
 9.999999999999999e-5
 0.0010999999999999998
 0.011099999999999997
 0.11109999999999996
 0.42566621465008614
 0.6819126503762631
 0.792916970687982
 0.9594900966375164
 1.1300613802984503

 2.9798087129844593
 3.245574751190571
 3.4952016139400106
 3.754834741991654
 4.010305712728722
 4.266803366748804
 4.522689267584815
 4.77759516700958
 5.0
u: 27-element Vector{Vector{Float64}}:
 [0.0]
 [4.999999999999998e-5]
 [0.0005499999999999998]
 [0.005549999999999997]
 [0.05554999999999997]
 [0.21283310732504301]
 [0.23388548114747057]
 [0.3903058781909205]
 [0.4717424649340658]
 [0.4387203313535804]

 [1.462630011239923]
 [1.5706650660821102]
 [1.7051156152029054]
 [1.8344790619426223]
 [1.9476035378906664]
 [2.1022816110316325]
 [2.191704521624201]
 [2.3680375317114795]
 [2.452550468144262]
julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.signal.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "CosineTest_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]], [0.5], label="Initial Condition for `signal.y`")
scatter!(plt, [df_case1.t[end]], [-0.49999], 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, "CosineTest_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]], [2.45255], label="Final Condition for `integrator.x`")

plt