Skip to content
LIBRARY
Sources.Tests.SineModes.md

Sources.Tests.SineModes

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

Four Sine instances share amplitude = 2, 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.

Each signal feeds an integrator so the test checks both the instantaneous output (at time = 0, in the pre-start region, and at the stop time, in the active region) and the accumulated integral.

Usage

BlockComponents.Sources.Tests.SineModes()

Behavior

julia
using BlockComponents #hide
using ModelingToolkit #hide
@named sys = BlockComponents.Sources.Tests.SineModes() #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 Sine source against analytically known values.

Four `Sine` instances share `amplitude = 2`, `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.

Each signal feeds an integrator so the test checks both the instantaneous output
(at `time = 0`, in the pre-start region, and at the stop time, in the active region)
and the accumulated integral.
"""
test component SineModes
  sig_default = BlockComponents.Sources.Sine(amplitude = 2, frequency = 1, start_time = 0.5, offset = 1, phase = pi / 4) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
      },
      "tags": []
    }
  }
  sig_gated = BlockComponents.Sources.Sine(amplitude = 2, frequency = 1, start_time = 0.5, offset = 1, phase = pi / 4, continuous = false) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 140, "x2": 120, "y2": 240, "rot": 0}
      },
      "tags": []
    }
  }
  sig_gated_abs = BlockComponents.Sources.Sine(amplitude = 2, frequency = 1, start_time = 0.5, offset = 1, phase = pi / 4, continuous = false, shift_time = 0) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 260, "x2": 120, "y2": 360, "rot": 0}
      },
      "tags": []
    }
  }
  sig_hold_abs = BlockComponents.Sources.Sine(amplitude = 2, frequency = 1, start_time = 0.5, offset = 1, phase = pi / 4, continuous = true, shift_time = 0) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 380, "x2": 120, "y2": 480, "rot": 0}
      },
      "tags": []
    }
  }
  int_default = BlockComponents.Continuous.Integrator() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 160, "y1": 20, "x2": 260, "y2": 120, "rot": 0}
      },
      "tags": []
    }
  }
  int_gated = BlockComponents.Continuous.Integrator() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 160, "y1": 140, "x2": 260, "y2": 240, "rot": 0}
      },
      "tags": []
    }
  }
  int_gated_abs = BlockComponents.Continuous.Integrator() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 160, "y1": 260, "x2": 260, "y2": 360, "rot": 0}
      },
      "tags": []
    }
  }
  int_hold_abs = BlockComponents.Continuous.Integrator() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 160, "y1": 380, "x2": 260, "y2": 480, "rot": 0}
      },
      "tags": []
    }
  }
relations
  connect(sig_default.y, int_default.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(sig_gated.y, int_gated.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(sig_gated_abs.y, int_gated_abs.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(sig_hold_abs.y, int_hold_abs.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
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.4142,
            "sig_gated.y": -0.4142,
            "sig_gated_abs.y": 2.4142,
            "sig_hold_abs.y": 2.4142,
            "int_default.x": 3.1573,
            "int_gated.x": 2.4502,
            "int_gated_abs.x": 1.5498,
            "int_hold_abs.x": 0.8427
          }
        }
      }
    }
  }
}
end
Flattened Source
dyad
"""
Verifies all operating modes of the Sine source against analytically known values.

Four `Sine` instances share `amplitude = 2`, `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.

Each signal feeds an integrator so the test checks both the instantaneous output
(at `time = 0`, in the pre-start region, and at the stop time, in the active region)
and the accumulated integral.
"""
test component SineModes
  sig_default = BlockComponents.Sources.Sine(amplitude = 2, frequency = 1, start_time = 0.5, offset = 1, phase = pi / 4) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
      },
      "tags": []
    }
  }
  sig_gated = BlockComponents.Sources.Sine(amplitude = 2, frequency = 1, start_time = 0.5, offset = 1, phase = pi / 4, continuous = false) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 140, "x2": 120, "y2": 240, "rot": 0}
      },
      "tags": []
    }
  }
  sig_gated_abs = BlockComponents.Sources.Sine(amplitude = 2, frequency = 1, start_time = 0.5, offset = 1, phase = pi / 4, continuous = false, shift_time = 0) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 260, "x2": 120, "y2": 360, "rot": 0}
      },
      "tags": []
    }
  }
  sig_hold_abs = BlockComponents.Sources.Sine(amplitude = 2, frequency = 1, start_time = 0.5, offset = 1, phase = pi / 4, continuous = true, shift_time = 0) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 380, "x2": 120, "y2": 480, "rot": 0}
      },
      "tags": []
    }
  }
  int_default = BlockComponents.Continuous.Integrator() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 160, "y1": 20, "x2": 260, "y2": 120, "rot": 0}
      },
      "tags": []
    }
  }
  int_gated = BlockComponents.Continuous.Integrator() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 160, "y1": 140, "x2": 260, "y2": 240, "rot": 0}
      },
      "tags": []
    }
  }
  int_gated_abs = BlockComponents.Continuous.Integrator() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 160, "y1": 260, "x2": 260, "y2": 360, "rot": 0}
      },
      "tags": []
    }
  }
  int_hold_abs = BlockComponents.Continuous.Integrator() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 160, "y1": 380, "x2": 260, "y2": 480, "rot": 0}
      },
      "tags": []
    }
  }
relations
  connect(sig_default.y, int_default.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(sig_gated.y, int_gated.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(sig_gated_abs.y, int_gated_abs.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(sig_hold_abs.y, int_hold_abs.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
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.4142,
            "sig_gated.y": -0.4142,
            "sig_gated_abs.y": 2.4142,
            "sig_hold_abs.y": 2.4142,
            "int_default.x": 3.1573,
            "int_gated.x": 2.4502,
            "int_gated_abs.x": 1.5498,
            "int_hold_abs.x": 0.8427
          }
        }
      }
    }
  }
}
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.SineModes()
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.SineModes_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.4142], 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.SineModes_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.4142], 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.SineModes_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]], [2.4142], 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.SineModes_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]], [2.4142], label="Final Condition for `sig_hold_abs.y`")
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>