LIBRARY
Sources.Tests.TorqueStep
Test for TorqueStep source, matching MSL ModelicaTest.Rotational.AllComponents topology.
TorqueStep applies offsetTorque=11 for t<1, then offsetTorque+stepTorque=33 for t≥1 to an inertia (J=3, initially at rest).
Phase 1 (0 ≤ t < 1): tau=11, a=11/3, w(t)=(11/3)t, phi(t)=(11/6)t² w(1) = 11/3 ≈ 3.667, phi(1) = 11/6 ≈ 1.833
Phase 2 (t ≥ 1): tau=33, a=11, w(t)=(11/3)+11(t-1), phi(t)=11/6+(11/3)(t-1)+(11/2)(t-1)² w(2) = 11/3+11 ≈ 14.667, phi(2) = 11/6+11/3+11/2 = 11/6+22/6+33/6 = 11
Usage
RotationalComponents.Sources.Tests.TorqueStep()
Behavior
julia
using RotationalComponents #hide
using ModelingToolkit #hide
@named sys = RotationalComponents.Sources.Tests.TorqueStep() #hide
full_equations(sys) #hide<< @example-block not executed in draft mode >>Source
dyad
"""
Test for TorqueStep source, matching MSL ModelicaTest.Rotational.AllComponents topology.
TorqueStep applies offsetTorque=11 for t<1, then offsetTorque+stepTorque=33 for t≥1
to an inertia (J=3, initially at rest).
Phase 1 (0 ≤ t < 1): tau=11, a=11/3, w(t)=(11/3)t, phi(t)=(11/6)t²
w(1) = 11/3 ≈ 3.667, phi(1) = 11/6 ≈ 1.833
Phase 2 (t ≥ 1): tau=33, a=11, w(t)=(11/3)+11(t-1), phi(t)=11/6+(11/3)(t-1)+(11/2)(t-1)²
w(2) = 11/3+11 ≈ 14.667, phi(2) = 11/6+11/3+11/2 = 11/6+22/6+33/6 = 11
"""
test component TorqueStep
"Fixed ground for source support"
fixed = RotationalComponents.Components.Fixed()
"Torque step source (stepTorque=22, offsetTorque=11, startTime=1)"
torque_step = RotationalComponents.Sources.TorqueStep(stepTorque = 22, offsetTorque = 11, startTime = 1)
"Inertia (J=3)"
inertia = RotationalComponents.Components.Inertia(J = 3)
relations
initial inertia.phi = 0
initial inertia.w = 0
connect(torque_step.spline, inertia.spline_a)
connect(torque_step.support, fixed.spline)
metadata {
"Dyad": {
"icons": {"default": "dyad://RotationalComponents/Example.svg"},
"tests": {
"case1": {
"stop": 2,
"atol": {"inertia.w": 0.01, "inertia.phi": 0.01},
"expect": {
"initial": {"inertia.phi": 0, "inertia.w": 0},
"final": {"inertia.w": 14.667, "inertia.phi": 11},
"signals": ["inertia.w", "inertia.phi", "torque_step.tau"]
}
}
}
}
}
endFlattened Source
dyad
"""
Test for TorqueStep source, matching MSL ModelicaTest.Rotational.AllComponents topology.
TorqueStep applies offsetTorque=11 for t<1, then offsetTorque+stepTorque=33 for t≥1
to an inertia (J=3, initially at rest).
Phase 1 (0 ≤ t < 1): tau=11, a=11/3, w(t)=(11/3)t, phi(t)=(11/6)t²
w(1) = 11/3 ≈ 3.667, phi(1) = 11/6 ≈ 1.833
Phase 2 (t ≥ 1): tau=33, a=11, w(t)=(11/3)+11(t-1), phi(t)=11/6+(11/3)(t-1)+(11/2)(t-1)²
w(2) = 11/3+11 ≈ 14.667, phi(2) = 11/6+11/3+11/2 = 11/6+22/6+33/6 = 11
"""
test component TorqueStep
"Fixed ground for source support"
fixed = RotationalComponents.Components.Fixed()
"Torque step source (stepTorque=22, offsetTorque=11, startTime=1)"
torque_step = RotationalComponents.Sources.TorqueStep(stepTorque = 22, offsetTorque = 11, startTime = 1)
"Inertia (J=3)"
inertia = RotationalComponents.Components.Inertia(J = 3)
relations
initial inertia.phi = 0
initial inertia.w = 0
connect(torque_step.spline, inertia.spline_a)
connect(torque_step.support, fixed.spline)
metadata {
"Dyad": {
"icons": {"default": "dyad://RotationalComponents/Example.svg"},
"tests": {
"case1": {
"stop": 2,
"atol": {"inertia.w": 0.01, "inertia.phi": 0.01},
"expect": {
"initial": {"inertia.phi": 0, "inertia.w": 0},
"final": {"inertia.w": 14.667, "inertia.phi": 11},
"signals": ["inertia.w", "inertia.phi", "torque_step.tau"]
}
}
}
}
}
endTest Cases
julia
using RotationalComponents
using DyadInterface: TransientAnalysis, rebuild_sol, ODEAlg
using ModelingToolkit: toggle_namespacing, get_initial_conditions, @named
using CSV, DataFrames, Plots
snapshotsdir = joinpath(dirname(dirname(pathof(RotationalComponents))), "test", "snapshots")<< @setup-block not executed in draft mode >>Test Case case1
julia
@named model_case1 = RotationalComponents.Sources.Tests.TorqueStep()
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-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.inertia.w])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "RotationalComponents.Sources.Tests.TorqueStep_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.inertia.w], width=2, label="Actual value of inertia.w")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of inertia.w")
end
scatter!(plt, [df_case1.t[1]], [0], label="Initial Condition for `inertia.w`")
scatter!(plt, [df_case1.t[end]], [14.667], label="Final Condition for `inertia.w`")<< @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.inertia.phi])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "RotationalComponents.Sources.Tests.TorqueStep_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.inertia.phi], width=2, label="Actual value of inertia.phi")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of inertia.phi")
end
scatter!(plt, [df_case1.t[1]], [0], label="Initial Condition for `inertia.phi`")
scatter!(plt, [df_case1.t[end]], [11], label="Final Condition for `inertia.phi`")<< @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.torque_step.tau])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "RotationalComponents.Sources.Tests.TorqueStep_case1_sig2.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.torque_step.tau], width=2, label="Actual value of torque_step.tau")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of torque_step.tau")
end<< @setup-block not executed in draft mode >>julia
plt<< @example-block not executed in draft mode >>Related
Examples
Experiments
Analyses
Tests