Skip to content
LIBRARY
Analog.Basic.Tests.TranslationalEMF.md

Analog.Basic.Tests.TranslationalEMF

Test circuit for TranslationalEMF driven by a sinusoidal voltage with a mass load.

A sine voltage source (amplitude=1 V, frequency=1 Hz) drives a TranslationalEMF (k=1 N/A). The mechanical flange is connected to a sliding mass (m=1 kg, horizontal, no gravity). The support is grounded via a Fixed component.

With an ideal voltage source and k=1:

  • vel = v/k = sin(2π·t)

  • s = (1 − cos(2π·t))/(2π)

  • a = 2π·cos(2π·t)

  • f = m·a = 2π·cos(2π·t)

  • i = −f/k = −2π·cos(2π·t)

Analogous to RotationalEMFTest in the standard library.

Usage

ElectricalComponents.Analog.Basic.Tests.TranslationalEMF()

Behavior

julia
using ElectricalComponents #hide
using ModelingToolkit #hide
@named sys = ElectricalComponents.Analog.Basic.Tests.TranslationalEMF() #hide
full_equations(sys) #hide
<< @example-block not executed in draft mode >>

Source

dyad
"""
Test circuit for TranslationalEMF driven by a sinusoidal voltage with a mass load.

A sine voltage source (amplitude=1 V, frequency=1 Hz) drives a TranslationalEMF
(k=1 N/A). The mechanical flange is connected to a sliding mass (m=1 kg,
horizontal, no gravity). The support is grounded via a Fixed component.

With an ideal voltage source and k=1:
- vel = v/k = sin(2π·t)
- s = (1 − cos(2π·t))/(2π)
- a = 2π·cos(2π·t)
- f = m·a = 2π·cos(2π·t)
- i = −f/k = −2π·cos(2π·t)

Analogous to `RotationalEMFTest` in the standard library.
"""
test component TranslationalEMF
  "Sine signal: amplitude=1, frequency=1 Hz"
  sine = BlockComponents.Sources.Sine(amplitude = 1, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 10, "y1": 490, "x2": 110, "y2": 590, "rot": 0}
      },
      "tags": []
    }
  }
  "Voltage source driven by sine"
  voltage_source = ElectricalComponents.Analog.Sources.VoltageSource() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 155, "y1": 485, "x2": 255, "y2": 385, "rot": 0}
      },
      "tags": []
    }
  }
  "Translational EMF with k=1 N/A"
  emf = ElectricalComponents.Analog.Basic.TranslationalEMF(k = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 220, "y1": 110, "x2": 320, "y2": 210, "rot": 0}
      },
      "tags": []
    }
  }
  "Electrical ground"
  ground = ElectricalComponents.Analog.Basic.Ground() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 420, "y1": 490, "x2": 520, "y2": 590, "rot": 0}
      },
      "tags": []
    }
  }
  "Mechanical ground for EMF support"
  fixed = TranslationalComponents.Components.Fixed() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 40, "y1": 180, "x2": 140, "y2": 280, "rot": 0}
      },
      "tags": []
    }
  }
  "Sliding mass (1 kg, horizontal)"
  mass = TranslationalComponents.Components.Mass(m = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 440, "y1": 110, "x2": 540, "y2": 210, "rot": 0}
      },
      "tags": []
    }
  }
relations
  initial emf.s = 0
  connect(sine.y, voltage_source.V) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 205, "y": 540}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(voltage_source.p, emf.p) {
    "Dyad": {
      "edges": [
        {
          "S": 1,
          "M": [{"x": 0, "y": 435}, {"x": 0, "y": 20}, {"x": 270, "y": 20}],
          "E": 2
        }
      ],
      "renderStyle": "standard"
    }
  }
  connect(voltage_source.n, emf.n) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [], "E": -1},
        {"S": -1, "M": [{"x": 335, "y": 305}, {"x": 270, "y": 305}], "E": 2}
      ],
      "junctions": [{"x": 335, "y": 435}],
      "renderStyle": "standard"
    }
  }
  connect(emf.n, ground.g) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [{"x": 270, "y": 305}, {"x": 335, "y": 305}], "E": -1},
        {"S": -1, "M": [{"x": 470, "y": 435}], "E": 2}
      ],
      "junctions": [{"x": 335, "y": 435}],
      "renderStyle": "standard"
    }
  }
  connect(fixed.flange, emf.support) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 90, "y": 160}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(emf.flange, mass.flange_a) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
  "Dyad": {
    "icons": {"default": "dyad://ElectricalComponents/Example.svg"},
    "tests": {
      "case1": {"stop": 1, "expect": {"signals": ["emf.v", "emf.i", "emf.s", "emf.vel"]}}
    }
  }
}
end
Flattened Source
dyad
"""
Test circuit for TranslationalEMF driven by a sinusoidal voltage with a mass load.

A sine voltage source (amplitude=1 V, frequency=1 Hz) drives a TranslationalEMF
(k=1 N/A). The mechanical flange is connected to a sliding mass (m=1 kg,
horizontal, no gravity). The support is grounded via a Fixed component.

With an ideal voltage source and k=1:
- vel = v/k = sin(2π·t)
- s = (1 − cos(2π·t))/(2π)
- a = 2π·cos(2π·t)
- f = m·a = 2π·cos(2π·t)
- i = −f/k = −2π·cos(2π·t)

Analogous to `RotationalEMFTest` in the standard library.
"""
test component TranslationalEMF
  "Sine signal: amplitude=1, frequency=1 Hz"
  sine = BlockComponents.Sources.Sine(amplitude = 1, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 10, "y1": 490, "x2": 110, "y2": 590, "rot": 0}
      },
      "tags": []
    }
  }
  "Voltage source driven by sine"
  voltage_source = ElectricalComponents.Analog.Sources.VoltageSource() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 155, "y1": 485, "x2": 255, "y2": 385, "rot": 0}
      },
      "tags": []
    }
  }
  "Translational EMF with k=1 N/A"
  emf = ElectricalComponents.Analog.Basic.TranslationalEMF(k = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 220, "y1": 110, "x2": 320, "y2": 210, "rot": 0}
      },
      "tags": []
    }
  }
  "Electrical ground"
  ground = ElectricalComponents.Analog.Basic.Ground() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 420, "y1": 490, "x2": 520, "y2": 590, "rot": 0}
      },
      "tags": []
    }
  }
  "Mechanical ground for EMF support"
  fixed = TranslationalComponents.Components.Fixed() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 40, "y1": 180, "x2": 140, "y2": 280, "rot": 0}
      },
      "tags": []
    }
  }
  "Sliding mass (1 kg, horizontal)"
  mass = TranslationalComponents.Components.Mass(m = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 440, "y1": 110, "x2": 540, "y2": 210, "rot": 0}
      },
      "tags": []
    }
  }
relations
  initial emf.s = 0
  connect(sine.y, voltage_source.V) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 205, "y": 540}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(voltage_source.p, emf.p) {
    "Dyad": {
      "edges": [
        {
          "S": 1,
          "M": [{"x": 0, "y": 435}, {"x": 0, "y": 20}, {"x": 270, "y": 20}],
          "E": 2
        }
      ],
      "renderStyle": "standard"
    }
  }
  connect(voltage_source.n, emf.n) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [], "E": -1},
        {"S": -1, "M": [{"x": 335, "y": 305}, {"x": 270, "y": 305}], "E": 2}
      ],
      "junctions": [{"x": 335, "y": 435}],
      "renderStyle": "standard"
    }
  }
  connect(emf.n, ground.g) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [{"x": 270, "y": 305}, {"x": 335, "y": 305}], "E": -1},
        {"S": -1, "M": [{"x": 470, "y": 435}], "E": 2}
      ],
      "junctions": [{"x": 335, "y": 435}],
      "renderStyle": "standard"
    }
  }
  connect(fixed.flange, emf.support) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 90, "y": 160}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(emf.flange, mass.flange_a) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
  "Dyad": {
    "icons": {"default": "dyad://ElectricalComponents/Example.svg"},
    "tests": {
      "case1": {"stop": 1, "expect": {"signals": ["emf.v", "emf.i", "emf.s", "emf.vel"]}}
    }
  }
}
end


Test Cases

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

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

Test Case case1

julia
@named model_case1 = ElectricalComponents.Analog.Basic.Tests.TranslationalEMF()
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 = 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.emf.v])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "ElectricalComponents.Analog.Basic.Tests.TranslationalEMF_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.emf.v], width=2, label="Actual value of emf.v")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of emf.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.emf.i])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "ElectricalComponents.Analog.Basic.Tests.TranslationalEMF_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.emf.i], width=2, label="Actual value of emf.i")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of emf.i")
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.emf.s])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "ElectricalComponents.Analog.Basic.Tests.TranslationalEMF_case1_sig2.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.emf.s], width=2, label="Actual value of emf.s")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of emf.s")
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.emf.vel])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "ElectricalComponents.Analog.Basic.Tests.TranslationalEMF_case1_sig3.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.emf.vel], width=2, label="Actual value of emf.vel")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of emf.vel")
end
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>