Skip to content
LIBRARY
Sources.Tests.ExpSineModes.md

Sources.Tests.ExpSineModes

Verifies all operating modes of the ExpSine source against analytically known values.

Four ExpSine instances share amplitude = 2, damping = 0.5, frequency = 1, start_time = 0.5, offset = 1, phase = π/4 (so 2π·frequency·start_time = π, half a cycle) and differ only in the continuous and shift_time parameters:

  • sig_default : new parameters left at their defaults (continuous = true, shift_time = start_time). Confirms the default reproduces the legacy value-continuous behavior (no breaking change).

  • sig_gated : continuous = false, shift_time = start_time — output holds at offset before start_time.

  • sig_gated_abs: continuous = false, shift_time = 0 — gated output with absolute time reference.

  • sig_hold_abs : continuous = true, shift_time = 0 — value-continuous output with absolute time reference.

The exponential damping envelope always uses (time - start_time), so the decay is identical across modes; only the sinusoidal phase reference and the pre-start value differ. Each signal feeds an integrator so the test checks both the instantaneous output and the accumulated integral.

Usage

BlockComponents.Sources.Tests.ExpSineModes()

Behavior

julia
using BlockComponents #hide
using ModelingToolkit #hide
@named sys = BlockComponents.Sources.Tests.ExpSineModes() #hide
let eqs = full_equations(sys); Base.length(eqs) > 25 ? nothing : eqs end #hide
<< @example-block not executed in draft mode >>

Source

dyad
"""
Verifies all operating modes of the ExpSine source against analytically known values.

Four `ExpSine` instances share `amplitude = 2`, `damping = 0.5`, `frequency = 1`,
`start_time = 0.5`, `offset = 1`, `phase = π/4` (so `2π·frequency·start_time = π`, half a
cycle) and differ only in the `continuous` and `shift_time` parameters:

- `sig_default`  : new parameters left at their defaults (`continuous = true`, `shift_time = start_time`).
                   Confirms the default reproduces the legacy value-continuous behavior (no breaking change).
- `sig_gated`    : `continuous = false`, `shift_time = start_time` — output holds at `offset` before `start_time`.
- `sig_gated_abs`: `continuous = false`, `shift_time = 0`          — gated output with absolute time reference.
- `sig_hold_abs` : `continuous = true`,  `shift_time = 0`          — value-continuous output with absolute time reference.

The exponential damping envelope always uses `(time - start_time)`, so the decay is
identical across modes; only the sinusoidal phase reference and the pre-start value differ.
Each signal feeds an integrator so the test checks both the instantaneous output and the
accumulated integral.
"""
test component ExpSineModes
  sig_default = BlockComponents.Sources.ExpSine(amplitude = 2, damping = 0.5, frequency = 1, start_time = 0.5, offset = 1, phase = pi / 4)
  sig_gated = BlockComponents.Sources.ExpSine(amplitude = 2, damping = 0.5, frequency = 1, start_time = 0.5, offset = 1, phase = pi / 4, continuous = false)
  sig_gated_abs = BlockComponents.Sources.ExpSine(amplitude = 2, damping = 0.5, frequency = 1, start_time = 0.5, offset = 1, phase = pi / 4, continuous = false, shift_time = 0)
  sig_hold_abs = BlockComponents.Sources.ExpSine(amplitude = 2, damping = 0.5, frequency = 1, start_time = 0.5, offset = 1, phase = pi / 4, continuous = true, shift_time = 0)
  int_default = BlockComponents.Continuous.Integrator()
  int_gated = BlockComponents.Continuous.Integrator()
  int_gated_abs = BlockComponents.Continuous.Integrator()
  int_hold_abs = BlockComponents.Continuous.Integrator()
relations
  connect(sig_default.y, int_default.u)
  connect(sig_gated.y, int_gated.u)
  connect(sig_gated_abs.y, int_gated_abs.u)
  connect(sig_hold_abs.y, int_hold_abs.u)
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 2,
        "abstol": 1e-8,
        "reltol": 1e-8,
        "atol": {
          "sig_default.y": 0.01,
          "sig_gated.y": 0.01,
          "sig_gated_abs.y": 0.01,
          "sig_hold_abs.y": 0.01,
          "int_default.x": 0.01,
          "int_gated.x": 0.01,
          "int_gated_abs.x": 0.01,
          "int_hold_abs.x": 0.01
        },
        "expect": {
          "initial": {
            "sig_default.y": 2.4142,
            "sig_gated.y": 1,
            "sig_gated_abs.y": 1,
            "sig_hold_abs.y": -0.4142
          },
          "signals": ["sig_default.y", "sig_gated.y", "sig_gated_abs.y", "sig_hold_abs.y"],
          "final": {
            "sig_default.y": 0.332,
            "sig_gated.y": 0.332,
            "sig_gated_abs.y": 1.668,
            "sig_hold_abs.y": 1.668,
            "int_default.x": 3.0626,
            "int_gated.x": 2.3555,
            "int_gated_abs.x": 1.6445,
            "int_hold_abs.x": 0.9374
          }
        }
      }
    }
  }
}
end
Flattened Source
dyad
"""
Verifies all operating modes of the ExpSine source against analytically known values.

Four `ExpSine` instances share `amplitude = 2`, `damping = 0.5`, `frequency = 1`,
`start_time = 0.5`, `offset = 1`, `phase = π/4` (so `2π·frequency·start_time = π`, half a
cycle) and differ only in the `continuous` and `shift_time` parameters:

- `sig_default`  : new parameters left at their defaults (`continuous = true`, `shift_time = start_time`).
                   Confirms the default reproduces the legacy value-continuous behavior (no breaking change).
- `sig_gated`    : `continuous = false`, `shift_time = start_time` — output holds at `offset` before `start_time`.
- `sig_gated_abs`: `continuous = false`, `shift_time = 0`          — gated output with absolute time reference.
- `sig_hold_abs` : `continuous = true`,  `shift_time = 0`          — value-continuous output with absolute time reference.

The exponential damping envelope always uses `(time - start_time)`, so the decay is
identical across modes; only the sinusoidal phase reference and the pre-start value differ.
Each signal feeds an integrator so the test checks both the instantaneous output and the
accumulated integral.
"""
test component ExpSineModes
  sig_default = BlockComponents.Sources.ExpSine(amplitude = 2, damping = 0.5, frequency = 1, start_time = 0.5, offset = 1, phase = pi / 4)
  sig_gated = BlockComponents.Sources.ExpSine(amplitude = 2, damping = 0.5, frequency = 1, start_time = 0.5, offset = 1, phase = pi / 4, continuous = false)
  sig_gated_abs = BlockComponents.Sources.ExpSine(amplitude = 2, damping = 0.5, frequency = 1, start_time = 0.5, offset = 1, phase = pi / 4, continuous = false, shift_time = 0)
  sig_hold_abs = BlockComponents.Sources.ExpSine(amplitude = 2, damping = 0.5, frequency = 1, start_time = 0.5, offset = 1, phase = pi / 4, continuous = true, shift_time = 0)
  int_default = BlockComponents.Continuous.Integrator()
  int_gated = BlockComponents.Continuous.Integrator()
  int_gated_abs = BlockComponents.Continuous.Integrator()
  int_hold_abs = BlockComponents.Continuous.Integrator()
relations
  connect(sig_default.y, int_default.u)
  connect(sig_gated.y, int_gated.u)
  connect(sig_gated_abs.y, int_gated_abs.u)
  connect(sig_hold_abs.y, int_hold_abs.u)
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 2,
        "abstol": 1e-8,
        "reltol": 1e-8,
        "atol": {
          "sig_default.y": 0.01,
          "sig_gated.y": 0.01,
          "sig_gated_abs.y": 0.01,
          "sig_hold_abs.y": 0.01,
          "int_default.x": 0.01,
          "int_gated.x": 0.01,
          "int_gated_abs.x": 0.01,
          "int_hold_abs.x": 0.01
        },
        "expect": {
          "initial": {
            "sig_default.y": 2.4142,
            "sig_gated.y": 1,
            "sig_gated_abs.y": 1,
            "sig_hold_abs.y": -0.4142
          },
          "signals": ["sig_default.y", "sig_gated.y", "sig_gated_abs.y", "sig_hold_abs.y"],
          "final": {
            "sig_default.y": 0.332,
            "sig_gated.y": 0.332,
            "sig_gated_abs.y": 1.668,
            "sig_hold_abs.y": 1.668,
            "int_default.x": 3.0626,
            "int_gated.x": 2.3555,
            "int_gated_abs.x": 1.6445,
            "int_hold_abs.x": 0.9374
          }
        }
      }
    }
  }
}
end


Test Cases

julia
using BlockComponents
using DyadInterface: TransientAnalysis, rebuild_sol, ODEAlg
using ModelingToolkit: toggle_namespacing, get_initial_conditions, @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 = BlockComponents.Sources.Tests.ExpSineModes()
model_case1 = toggle_namespacing(model_case1, false)

model_case1 = toggle_namespacing(model_case1, true)
result_case1 = TransientAnalysis(; model = model_case1, alg = ODEAlg.Auto(), start = 0e+0, stop = 2e+0, abstol=1e-8, reltol=1e-8)
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.sig_default.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Sources.Tests.ExpSineModes_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.sig_default.y], width=2, label="Actual value of sig_default.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of sig_default.y")
end
scatter!(plt, [df_case1.t[1]], [2.4142], label="Initial Condition for `sig_default.y`")
scatter!(plt, [df_case1.t[end]], [0.332], label="Final Condition for `sig_default.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.sig_gated.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Sources.Tests.ExpSineModes_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.sig_gated.y], width=2, label="Actual value of sig_gated.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of sig_gated.y")
end
scatter!(plt, [df_case1.t[1]], [1], label="Initial Condition for `sig_gated.y`")
scatter!(plt, [df_case1.t[end]], [0.332], label="Final Condition for `sig_gated.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.sig_gated_abs.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Sources.Tests.ExpSineModes_case1_sig2.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.sig_gated_abs.y], width=2, label="Actual value of sig_gated_abs.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of sig_gated_abs.y")
end
scatter!(plt, [df_case1.t[1]], [1], label="Initial Condition for `sig_gated_abs.y`")
scatter!(plt, [df_case1.t[end]], [1.668], label="Final Condition for `sig_gated_abs.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.sig_hold_abs.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Sources.Tests.ExpSineModes_case1_sig3.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.sig_hold_abs.y], width=2, label="Actual value of sig_hold_abs.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of sig_hold_abs.y")
end
scatter!(plt, [df_case1.t[1]], [-0.4142], label="Initial Condition for `sig_hold_abs.y`")
scatter!(plt, [df_case1.t[end]], [1.668], label="Final Condition for `sig_hold_abs.y`")
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>