Skip to content
LIBRARY
Sources.Tests.TestBraking.md

Sources.Tests.TestBraking

Test for braking torque sources, matching MSL ModelicaTest.Rotational.TestBraking.

Five inertias (J=1) each start at w=100 rad/s and are braked by a different source. EddyCurrentTorque is omitted (depends on ThermalComponents).

Expected behavior (all with TorqueDirection=true, the default):

  1. LinearSpeedDependentTorque: tau = -|w| → w(t) = 100·exp(-t) → w(2) ≈ 13.534

  2. QuadraticSpeedDependentTorque: tau = -w²/100 → w = 100/(1+t) → w(2) ≈ 33.333

  3. ConstantTorque: tau = -100 → w = 100-100t → w(2) = -100

  4. SignTorque (Exp): tau ≈ -100·sign(w) → brakes to 0, then regularizes near w=0

  5. InverseSpeedDependentTorque: tau = -10000/|w| → w = 100·√(1-2t) → w hits 0 at t=0.5

Usage

RotationalComponents.Sources.Tests.TestBraking()

Behavior

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

Source

dyad
"""
Test for braking torque sources, matching MSL ModelicaTest.Rotational.TestBraking.

Five inertias (J=1) each start at w=100 rad/s and are braked by a different
source. EddyCurrentTorque is omitted (depends on ThermalComponents).

Expected behavior (all with TorqueDirection=true, the default):
  1. LinearSpeedDependentTorque: tau = -|w| → w(t) = 100·exp(-t) → w(2) ≈ 13.534
  2. QuadraticSpeedDependentTorque: tau = -w²/100 → w = 100/(1+t) → w(2) ≈ 33.333
  3. ConstantTorque: tau = -100 → w = 100-100t → w(2) = -100
  4. SignTorque (Exp): tau ≈ -100·sign(w) → brakes to 0, then regularizes near w=0
  5. InverseSpeedDependentTorque: tau = -10000/|w| → w = 100·√(1-2t) → w hits 0 at t=0.5
"""
test component TestBraking
  "Fixed ground for all source supports"
  fixed = RotationalComponents.Components.Fixed() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": -90, "y1": 760, "x2": 10, "y2": 860, "rot": 90}
      },
      "tags": []
    }
  }
  "Inertia 1 for linear speed dependent braking"
  inertia1 = RotationalComponents.Components.Inertia(J = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 560, "y1": 720, "x2": 660, "y2": 820, "rot": 0}
      },
      "tags": []
    }
  }
  "Linear speed dependent torque (tau_nominal=-100, w_nominal=100)"
  linear_speed = RotationalComponents.Sources.LinearSpeedDependentTorque(tau_nominal = -100, w_nominal = 100) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 190, "y1": 720, "x2": 290, "y2": 820, "rot": 0}
      },
      "tags": []
    }
  }
  "Inertia 2 for quadratic speed dependent braking"
  inertia2 = RotationalComponents.Components.Inertia(J = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 570, "y1": 450, "x2": 670, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  "Quadratic speed dependent torque (tau_nominal=-100, w_nominal=100)"
  quadratic_speed = RotationalComponents.Sources.QuadraticSpeedDependentTorque(tau_nominal = -100, w_nominal = 100) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 180, "y1": 450, "x2": 280, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  "Inertia 3 for constant torque braking"
  inertia3 = RotationalComponents.Components.Inertia(J = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 560, "y1": 950, "x2": 660, "y2": 1050, "rot": 0}
      },
      "tags": []
    }
  }
  "Constant torque (tau_constant=-100)"
  constant_torque = RotationalComponents.Sources.ConstantTorque(tau_constant = -100) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 190, "y1": 950, "x2": 290, "y2": 1050, "rot": 0}
      },
      "tags": []
    }
  }
  "Inertia 4 for sign torque braking"
  inertia4 = RotationalComponents.Components.Inertia(J = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 560, "y1": 200, "x2": 660, "y2": 300, "rot": 0}
      },
      "tags": []
    }
  }
  "Sign torque with Exp regularization (tau_nominal=-100, w0=1)"
  sign_torque = RotationalComponents.Sources.SignTorque(tau_nominal = -100, w0 = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 180, "y1": 200, "x2": 280, "y2": 300, "rot": 0}
      },
      "tags": []
    }
  }
  "Inertia 5 for inverse speed dependent braking"
  inertia5 = RotationalComponents.Components.Inertia(J = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 550, "y1": 0, "x2": 650, "y2": 100, "rot": 0}
      },
      "tags": []
    }
  }
  "Inverse speed dependent torque (tau_nominal=-100, w_nominal=100, w0=1)"
  inverse_speed = RotationalComponents.Sources.InverseSpeedDependentTorque(tau_nominal = -100, w_nominal = 100, w0 = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 180, "y1": 0, "x2": 280, "y2": 100, "rot": 0}
      },
      "tags": []
    }
  }
relations
  initial inertia1.phi = 0
  initial inertia1.w = 100
  initial inertia2.phi = 0
  initial inertia2.w = 100
  initial inertia3.phi = 0
  initial inertia3.w = 100
  initial inertia4.phi = 0
  initial inertia4.w = 100
  initial inertia5.phi = 0
  initial inertia5.w = 100
  connect(inertia1.spline_b, linear_speed.spline) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(linear_speed.support, fixed.spline) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [{"x": 240, "y": 870}, {"x": 50, "y": 870}], "E": -1},
        {"S": -1, "M": [{"x": 50, "y": 810}], "E": 2}
      ],
      "junctions": [{"x": 50, "y": 750}],
      "renderStyle": "standard"
    }
  }
  connect(inertia2.spline_b, quadratic_speed.spline) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(quadratic_speed.support, fixed.spline) {
    "Dyad": {
      "edges": [
        {
          "S": 1,
          "M": [{"x": 230, "y": 670}, {"x": 50, "y": 670}, {"x": 50, "y": 810}],
          "E": 2
        }
      ],
      "renderStyle": "standard"
    }
  }
  connect(inertia3.spline_b, constant_torque.spline) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(constant_torque.support, fixed.spline) {
    "Dyad": {
      "edges": [
        {
          "S": 1,
          "M": [{"x": 240, "y": 1120}, {"x": 50, "y": 1120}, {"x": 50, "y": 810}],
          "E": 2
        }
      ],
      "renderStyle": "standard"
    }
  }
  connect(inertia4.spline_b, sign_torque.spline) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(sign_torque.support, fixed.spline) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [{"x": 230, "y": 380}, {"x": 50, "y": 380}], "E": -1},
        {"S": -1, "M": [{"x": 50, "y": 810}], "E": 2}
      ],
      "junctions": [{"x": 50, "y": 500}],
      "renderStyle": "standard"
    }
  }
  connect(inertia5.spline_b, inverse_speed.spline) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(inverse_speed.support, fixed.spline) {
    "Dyad": {
      "edges": [
        {
          "S": 1,
          "M": [{"x": 230, "y": 160}, {"x": 50, "y": 160}, {"x": 50, "y": 810}],
          "E": -1
        },
        {"S": -1, "M": [], "E": 2}
      ],
      "junctions": [{"x": 20, "y": 810}],
      "renderStyle": "standard"
    }
  }
metadata {
  "Dyad": {
    "icons": {"default": "dyad://RotationalComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 2,
        "abstol": 1e-8,
        "reltol": 1e-8,
        "atol": {"inertia1.w": 0.01, "inertia2.w": 0.01, "inertia3.w": 0.01},
        "expect": {
          "initial": {
            "inertia1.w": 100,
            "inertia2.w": 100,
            "inertia3.w": 100,
            "inertia4.w": 100,
            "inertia5.w": 100,
            "inertia1.phi": 0,
            "inertia2.phi": 0,
            "inertia3.phi": 0,
            "inertia4.phi": 0,
            "inertia5.phi": 0
          },
          "final": {"inertia1.w": 13.534, "inertia2.w": 33.333, "inertia3.w": -100},
          "signals": ["inertia1.w", "inertia2.w", "inertia3.w", "inertia4.w", "inertia5.w"]
        }
      }
    }
  }
}
end
Flattened Source
dyad
"""
Test for braking torque sources, matching MSL ModelicaTest.Rotational.TestBraking.

Five inertias (J=1) each start at w=100 rad/s and are braked by a different
source. EddyCurrentTorque is omitted (depends on ThermalComponents).

Expected behavior (all with TorqueDirection=true, the default):
  1. LinearSpeedDependentTorque: tau = -|w| → w(t) = 100·exp(-t) → w(2) ≈ 13.534
  2. QuadraticSpeedDependentTorque: tau = -w²/100 → w = 100/(1+t) → w(2) ≈ 33.333
  3. ConstantTorque: tau = -100 → w = 100-100t → w(2) = -100
  4. SignTorque (Exp): tau ≈ -100·sign(w) → brakes to 0, then regularizes near w=0
  5. InverseSpeedDependentTorque: tau = -10000/|w| → w = 100·√(1-2t) → w hits 0 at t=0.5
"""
test component TestBraking
  "Fixed ground for all source supports"
  fixed = RotationalComponents.Components.Fixed() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": -90, "y1": 760, "x2": 10, "y2": 860, "rot": 90}
      },
      "tags": []
    }
  }
  "Inertia 1 for linear speed dependent braking"
  inertia1 = RotationalComponents.Components.Inertia(J = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 560, "y1": 720, "x2": 660, "y2": 820, "rot": 0}
      },
      "tags": []
    }
  }
  "Linear speed dependent torque (tau_nominal=-100, w_nominal=100)"
  linear_speed = RotationalComponents.Sources.LinearSpeedDependentTorque(tau_nominal = -100, w_nominal = 100) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 190, "y1": 720, "x2": 290, "y2": 820, "rot": 0}
      },
      "tags": []
    }
  }
  "Inertia 2 for quadratic speed dependent braking"
  inertia2 = RotationalComponents.Components.Inertia(J = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 570, "y1": 450, "x2": 670, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  "Quadratic speed dependent torque (tau_nominal=-100, w_nominal=100)"
  quadratic_speed = RotationalComponents.Sources.QuadraticSpeedDependentTorque(tau_nominal = -100, w_nominal = 100) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 180, "y1": 450, "x2": 280, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  "Inertia 3 for constant torque braking"
  inertia3 = RotationalComponents.Components.Inertia(J = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 560, "y1": 950, "x2": 660, "y2": 1050, "rot": 0}
      },
      "tags": []
    }
  }
  "Constant torque (tau_constant=-100)"
  constant_torque = RotationalComponents.Sources.ConstantTorque(tau_constant = -100) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 190, "y1": 950, "x2": 290, "y2": 1050, "rot": 0}
      },
      "tags": []
    }
  }
  "Inertia 4 for sign torque braking"
  inertia4 = RotationalComponents.Components.Inertia(J = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 560, "y1": 200, "x2": 660, "y2": 300, "rot": 0}
      },
      "tags": []
    }
  }
  "Sign torque with Exp regularization (tau_nominal=-100, w0=1)"
  sign_torque = RotationalComponents.Sources.SignTorque(tau_nominal = -100, w0 = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 180, "y1": 200, "x2": 280, "y2": 300, "rot": 0}
      },
      "tags": []
    }
  }
  "Inertia 5 for inverse speed dependent braking"
  inertia5 = RotationalComponents.Components.Inertia(J = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 550, "y1": 0, "x2": 650, "y2": 100, "rot": 0}
      },
      "tags": []
    }
  }
  "Inverse speed dependent torque (tau_nominal=-100, w_nominal=100, w0=1)"
  inverse_speed = RotationalComponents.Sources.InverseSpeedDependentTorque(tau_nominal = -100, w_nominal = 100, w0 = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 180, "y1": 0, "x2": 280, "y2": 100, "rot": 0}
      },
      "tags": []
    }
  }
relations
  initial inertia1.phi = 0
  initial inertia1.w = 100
  initial inertia2.phi = 0
  initial inertia2.w = 100
  initial inertia3.phi = 0
  initial inertia3.w = 100
  initial inertia4.phi = 0
  initial inertia4.w = 100
  initial inertia5.phi = 0
  initial inertia5.w = 100
  connect(inertia1.spline_b, linear_speed.spline) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(linear_speed.support, fixed.spline) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [{"x": 240, "y": 870}, {"x": 50, "y": 870}], "E": -1},
        {"S": -1, "M": [{"x": 50, "y": 810}], "E": 2}
      ],
      "junctions": [{"x": 50, "y": 750}],
      "renderStyle": "standard"
    }
  }
  connect(inertia2.spline_b, quadratic_speed.spline) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(quadratic_speed.support, fixed.spline) {
    "Dyad": {
      "edges": [
        {
          "S": 1,
          "M": [{"x": 230, "y": 670}, {"x": 50, "y": 670}, {"x": 50, "y": 810}],
          "E": 2
        }
      ],
      "renderStyle": "standard"
    }
  }
  connect(inertia3.spline_b, constant_torque.spline) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(constant_torque.support, fixed.spline) {
    "Dyad": {
      "edges": [
        {
          "S": 1,
          "M": [{"x": 240, "y": 1120}, {"x": 50, "y": 1120}, {"x": 50, "y": 810}],
          "E": 2
        }
      ],
      "renderStyle": "standard"
    }
  }
  connect(inertia4.spline_b, sign_torque.spline) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(sign_torque.support, fixed.spline) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [{"x": 230, "y": 380}, {"x": 50, "y": 380}], "E": -1},
        {"S": -1, "M": [{"x": 50, "y": 810}], "E": 2}
      ],
      "junctions": [{"x": 50, "y": 500}],
      "renderStyle": "standard"
    }
  }
  connect(inertia5.spline_b, inverse_speed.spline) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(inverse_speed.support, fixed.spline) {
    "Dyad": {
      "edges": [
        {
          "S": 1,
          "M": [{"x": 230, "y": 160}, {"x": 50, "y": 160}, {"x": 50, "y": 810}],
          "E": -1
        },
        {"S": -1, "M": [], "E": 2}
      ],
      "junctions": [{"x": 20, "y": 810}],
      "renderStyle": "standard"
    }
  }
metadata {
  "Dyad": {
    "icons": {"default": "dyad://RotationalComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 2,
        "abstol": 1e-8,
        "reltol": 1e-8,
        "atol": {"inertia1.w": 0.01, "inertia2.w": 0.01, "inertia3.w": 0.01},
        "expect": {
          "initial": {
            "inertia1.w": 100,
            "inertia2.w": 100,
            "inertia3.w": 100,
            "inertia4.w": 100,
            "inertia5.w": 100,
            "inertia1.phi": 0,
            "inertia2.phi": 0,
            "inertia3.phi": 0,
            "inertia4.phi": 0,
            "inertia5.phi": 0
          },
          "final": {"inertia1.w": 13.534, "inertia2.w": 33.333, "inertia3.w": -100},
          "signals": ["inertia1.w", "inertia2.w", "inertia3.w", "inertia4.w", "inertia5.w"]
        }
      }
    }
  }
}
end


Test 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.TestBraking()
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.inertia1.w])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "RotationalComponents.Sources.Tests.TestBraking_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.inertia1.w], width=2, label="Actual value of inertia1.w")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of inertia1.w")
end
scatter!(plt, [df_case1.t[1]], [100], label="Initial Condition for `inertia1.w`")
scatter!(plt, [df_case1.t[end]], [13.534], label="Final Condition for `inertia1.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.inertia2.w])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "RotationalComponents.Sources.Tests.TestBraking_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.inertia2.w], width=2, label="Actual value of inertia2.w")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of inertia2.w")
end
scatter!(plt, [df_case1.t[1]], [100], label="Initial Condition for `inertia2.w`")
scatter!(plt, [df_case1.t[end]], [33.333], label="Final Condition for `inertia2.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.inertia3.w])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "RotationalComponents.Sources.Tests.TestBraking_case1_sig2.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.inertia3.w], width=2, label="Actual value of inertia3.w")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of inertia3.w")
end
scatter!(plt, [df_case1.t[1]], [100], label="Initial Condition for `inertia3.w`")
scatter!(plt, [df_case1.t[end]], [-100], label="Final Condition for `inertia3.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.inertia4.w])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "RotationalComponents.Sources.Tests.TestBraking_case1_sig3.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.inertia4.w], width=2, label="Actual value of inertia4.w")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of inertia4.w")
end
scatter!(plt, [df_case1.t[1]], [100], label="Initial Condition for `inertia4.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.inertia5.w])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "RotationalComponents.Sources.Tests.TestBraking_case1_sig4.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.inertia5.w], width=2, label="Actual value of inertia5.w")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of inertia5.w")
end
scatter!(plt, [df_case1.t[1]], [100], label="Initial Condition for `inertia5.w`")
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>