Skip to content
LIBRARY
Sources.Tests.Speed.md

Sources.Tests.Speed

Test of the Speed source component with all ReferenceType enum variants.

Three Speed instances are driven by a common Sine reference (amplitude=1 m/s, frequency=1 Hz) and each drives a Mass (m=1 kg) with support grounded to Fixed:

  • speed_filtered: default Filtered mode (f_crit=50 Hz)

  • speed_exact: Exact mode — flange velocity equals input directly

  • speed_filtered_low: Filtered mode with low critical frequency (f_crit=5 Hz) to show visible filtering

Usage

TranslationalComponents.Sources.Tests.Speed()

Behavior

julia
using TranslationalComponents #hide
using ModelingToolkit #hide
@named sys = TranslationalComponents.Sources.Tests.Speed() #hide
full_equations(sys) #hide
<< @example-block not executed in draft mode >>

Source

dyad
"""
Test of the Speed source component with all ReferenceType enum variants.

Three Speed instances are driven by a common Sine reference (amplitude=1 m/s, frequency=1 Hz)
and each drives a Mass (m=1 kg) with support grounded to Fixed:

- speed_filtered: default Filtered mode (f_crit=50 Hz)
- speed_exact: Exact mode — flange velocity equals input directly
- speed_filtered_low: Filtered mode with low critical frequency (f_crit=5 Hz) to show visible filtering
"""
test component Speed
  "Sine reference speed signal (amplitude=1 m/s, frequency=1 Hz)"
  sine = BlockComponents.Sources.Sine(amplitude = 1.0, frequency = 1.0)
  "Speed source — Filtered with default filter (f_crit=50 Hz)"
  speed_filtered = TranslationalComponents.Sources.Speed()
  "Mass driven by filtered speed source"
  mass_filtered = TranslationalComponents.Components.Mass(m = 1.0)
  "Speed source — Exact (flange tracks input directly)"
  speed_exact = TranslationalComponents.Sources.Speed(ref_type = TranslationalComponents.Sources.ReferenceType.Exact())
  "Mass driven by exact speed source"
  mass_exact = TranslationalComponents.Components.Mass(m = 1.0)
  "Speed source — Filtered with low critical frequency (f_crit=5 Hz)"
  speed_filtered_low = TranslationalComponents.Sources.Speed(ref_type = TranslationalComponents.Sources.ReferenceType.Filtered(f_crit = 5.0))
  "Mass driven by low-f_crit filtered speed source"
  mass_filtered_low = TranslationalComponents.Components.Mass(m = 1.0)
  "Fixed ground for all supports"
  ground = TranslationalComponents.Components.Fixed()
relations
  # --- Filtered (default) ---
  connect(sine.y, speed_filtered.v_ref)
  connect(speed_filtered.flange, mass_filtered.flange_a)
  connect(speed_filtered.support, ground.flange)
  # --- Exact ---
  connect(sine.y, speed_exact.v_ref)
  connect(speed_exact.flange, mass_exact.flange_a)
  connect(speed_exact.support, ground.flange)
  # --- Filtered low f_crit ---
  connect(sine.y, speed_filtered_low.v_ref)
  connect(speed_filtered_low.flange, mass_filtered_low.flange_a)
  connect(speed_filtered_low.support, ground.flange)
metadata {
  "Dyad": {
    "icons": {"default": "dyad://TranslationalComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 1.1,
        "atol": {
          "mass_exact.v": 0.001,
          "mass_exact.s": 0.001,
          "mass_filtered.v": 0.001,
          "mass_filtered.s": 0.001,
          "mass_filtered_low.v": 0.01,
          "mass_filtered_low.s": 0.01
        },
        "expect": {
          "final": {
            "mass_exact.v": 0.5877852522924736,
            "mass_exact.s": 0.030395889391774417,
            "mass_filtered.v": 0.5713763618836489,
            "mass_filtered.s": 0.02857703022540215
          },
          "signals": [
            "mass_exact.v",
            "mass_exact.s",
            "mass_filtered.v",
            "mass_filtered.s",
            "mass_filtered_low.v",
            "mass_filtered_low.s"
          ]
        }
      }
    }
  }
}
end
Flattened Source
dyad
"""
Test of the Speed source component with all ReferenceType enum variants.

Three Speed instances are driven by a common Sine reference (amplitude=1 m/s, frequency=1 Hz)
and each drives a Mass (m=1 kg) with support grounded to Fixed:

- speed_filtered: default Filtered mode (f_crit=50 Hz)
- speed_exact: Exact mode — flange velocity equals input directly
- speed_filtered_low: Filtered mode with low critical frequency (f_crit=5 Hz) to show visible filtering
"""
test component Speed
  "Sine reference speed signal (amplitude=1 m/s, frequency=1 Hz)"
  sine = BlockComponents.Sources.Sine(amplitude = 1.0, frequency = 1.0)
  "Speed source — Filtered with default filter (f_crit=50 Hz)"
  speed_filtered = TranslationalComponents.Sources.Speed()
  "Mass driven by filtered speed source"
  mass_filtered = TranslationalComponents.Components.Mass(m = 1.0)
  "Speed source — Exact (flange tracks input directly)"
  speed_exact = TranslationalComponents.Sources.Speed(ref_type = TranslationalComponents.Sources.ReferenceType.Exact())
  "Mass driven by exact speed source"
  mass_exact = TranslationalComponents.Components.Mass(m = 1.0)
  "Speed source — Filtered with low critical frequency (f_crit=5 Hz)"
  speed_filtered_low = TranslationalComponents.Sources.Speed(ref_type = TranslationalComponents.Sources.ReferenceType.Filtered(f_crit = 5.0))
  "Mass driven by low-f_crit filtered speed source"
  mass_filtered_low = TranslationalComponents.Components.Mass(m = 1.0)
  "Fixed ground for all supports"
  ground = TranslationalComponents.Components.Fixed()
relations
  # --- Filtered (default) ---
  connect(sine.y, speed_filtered.v_ref)
  connect(speed_filtered.flange, mass_filtered.flange_a)
  connect(speed_filtered.support, ground.flange)
  # --- Exact ---
  connect(sine.y, speed_exact.v_ref)
  connect(speed_exact.flange, mass_exact.flange_a)
  connect(speed_exact.support, ground.flange)
  # --- Filtered low f_crit ---
  connect(sine.y, speed_filtered_low.v_ref)
  connect(speed_filtered_low.flange, mass_filtered_low.flange_a)
  connect(speed_filtered_low.support, ground.flange)
metadata {
  "Dyad": {
    "icons": {"default": "dyad://TranslationalComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 1.1,
        "atol": {
          "mass_exact.v": 0.001,
          "mass_exact.s": 0.001,
          "mass_filtered.v": 0.001,
          "mass_filtered.s": 0.001,
          "mass_filtered_low.v": 0.01,
          "mass_filtered_low.s": 0.01
        },
        "expect": {
          "final": {
            "mass_exact.v": 0.5877852522924736,
            "mass_exact.s": 0.030395889391774417,
            "mass_filtered.v": 0.5713763618836489,
            "mass_filtered.s": 0.02857703022540215
          },
          "signals": [
            "mass_exact.v",
            "mass_exact.s",
            "mass_filtered.v",
            "mass_filtered.s",
            "mass_filtered_low.v",
            "mass_filtered_low.s"
          ]
        }
      }
    }
  }
}
end


Test Cases

julia
using TranslationalComponents
using DyadInterface: TransientAnalysis, rebuild_sol, ODEAlg
using ModelingToolkit: toggle_namespacing, get_initial_conditions, @named
using CSV, DataFrames, Plots

snapshotsdir = joinpath(dirname(dirname(pathof(TranslationalComponents))), "test", "snapshots")
<< @setup-block not executed in draft mode >>

Test Case case1

julia
@named model_case1 = TranslationalComponents.Sources.Tests.Speed()
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 = 1.1e+0, abstol=1e-6, reltol=1e-6)
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.mass_exact.v])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Sources.Tests.Speed_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.mass_exact.v], width=2, label="Actual value of mass_exact.v")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of mass_exact.v")
end
scatter!(plt, [df_case1.t[end]], [0.5877852522924736], label="Final Condition for `mass_exact.v`")
<< @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.mass_exact.s])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Sources.Tests.Speed_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.mass_exact.s], width=2, label="Actual value of mass_exact.s")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of mass_exact.s")
end
scatter!(plt, [df_case1.t[end]], [0.030395889391774417], label="Final Condition for `mass_exact.s`")
<< @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.mass_filtered.v])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Sources.Tests.Speed_case1_sig2.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.mass_filtered.v], width=2, label="Actual value of mass_filtered.v")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of mass_filtered.v")
end
scatter!(plt, [df_case1.t[end]], [0.5713763618836489], label="Final Condition for `mass_filtered.v`")
<< @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.mass_filtered.s])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Sources.Tests.Speed_case1_sig3.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.mass_filtered.s], width=2, label="Actual value of mass_filtered.s")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of mass_filtered.s")
end
scatter!(plt, [df_case1.t[end]], [0.02857703022540215], label="Final Condition for `mass_filtered.s`")
<< @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.mass_filtered_low.v])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Sources.Tests.Speed_case1_sig4.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.mass_filtered_low.v], width=2, label="Actual value of mass_filtered_low.v")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of mass_filtered_low.v")
end
<< @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.mass_filtered_low.s])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Sources.Tests.Speed_case1_sig5.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.mass_filtered_low.s], width=2, label="Actual value of mass_filtered_low.s")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of mass_filtered_low.s")
end
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>