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

Analog.Basic.Tests.MTransformer

Test circuit for MTransformer with N=2.

Replicates the primary-side circuit from the MSL CompareTransformers example: SineVoltage → R1 → Transformer primary → Ground (primary side) Transformer secondary → R2 → Load → Ground (secondary side)

With these parameters the MTransformer(N=2) should behave identically to the MSL Basic.Transformer(L1, L2, M).

Parameters (from MSL CompareTransformers): f = 10 Hz, Vpeak = 0.1 V, Vdc = 0.1 V, phase = pi/2 n = 2 (turns ratio) R1 = 0.01 Ω, R2 = 0.0025 Ω, RL = 0.25 Ω L1 ≈ 0.16 H, L2 ≈ 0.04 H, M ≈ 0.07958 H

Usage

ElectricalComponents.Analog.Basic.Tests.MTransformer()

Behavior

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

Source

dyad
"""
Test circuit for MTransformer with N=2.

Replicates the primary-side circuit from the MSL CompareTransformers example:
  SineVoltage → R1 → Transformer primary → Ground (primary side)
  Transformer secondary → R2 → Load → Ground (secondary side)

With these parameters the MTransformer(N=2) should behave identically
to the MSL Basic.Transformer(L1, L2, M).

Parameters (from MSL CompareTransformers):
  f = 10 Hz, Vpeak = 0.1 V, Vdc = 0.1 V, phase = pi/2
  n = 2 (turns ratio)
  R1 = 0.01 Ω, R2 = 0.0025 Ω, RL = 0.25 Ω
  L1 ≈ 0.16 H, L2 ≈ 0.04 H, M ≈ 0.07958 H
"""
test component MTransformer
  # Sine voltage source (primary side)
  sine = BlockComponents.Sources.Sine(offset = 0.1, amplitude = 0.1, frequency = 10.0, phase = 1.5707963267948966) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
      },
      "tags": []
    }
  }
  source = ElectricalComponents.Analog.Sources.VoltageSource() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 155, "y1": 125, "x2": 255, "y2": 225, "rot": 0}
      },
      "tags": []
    }
  }
  # Primary winding resistance
  r1 = ElectricalComponents.Analog.Basic.Resistor(R = 0.01) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 285, "y1": 236, "x2": 385, "y2": 336, "rot": 0}
      },
      "tags": []
    }
  }
  # The MTransformer with N=2
  transformer = ElectricalComponents.Analog.Basic.MTransformer(N = 2, L = [0.15996, 0.07958, 0.03999]) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 414, "y1": 236, "x2": 514, "y2": 336, "rot": 0}
      },
      "tags": []
    }
  }
  # Secondary winding resistance
  r2 = ElectricalComponents.Analog.Basic.Resistor(R = 0.0025) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 630, "y1": 500, "x2": 730, "y2": 600, "rot": 0}
      },
      "tags": []
    }
  }
  # Load resistor
  load = ElectricalComponents.Analog.Basic.Resistor(R = 0.25) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 155, "y1": 356, "x2": 255, "y2": 456, "rot": 0}
      },
      "tags": []
    }
  }
  # Separate grounds for primary and secondary (galvanic isolation)
  gnd1 = ElectricalComponents.Analog.Basic.Ground() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 620, "y1": 350, "x2": 720, "y2": 450, "rot": 0}
      },
      "tags": []
    }
  }
  gnd2 = ElectricalComponents.Analog.Basic.Ground() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 290, "y1": 500, "x2": 390, "y2": 600, "rot": 0}
      },
      "tags": []
    }
  }
relations
  # Drive voltage source with sine
  connect(sine.y, source.V) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 205, "y": 70}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  # Primary side: source → R1 → transformer winding 1 → ground
  connect(source.p, r1.p) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 140, "y": 175}, {"x": 140, "y": 286}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(r1.n, transformer.p[1]) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(transformer.n[1], gnd1.g, source.n) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [], "E": -1},
        {"S": -1, "M": [{"x": 670, "y": 286}], "E": 2},
        {
          "S": -1,
          "M": [{"x": 540, "y": 246}, {"x": 600, "y": 246}, {"x": 600, "y": 175}],
          "E": 3
        }
      ],
      "junctions": [{"x": 540, "y": 286}],
      "renderStyle": "standard"
    }
  }
  # Secondary side: transformer winding 2 → R2 → load → ground
  connect(transformer.p[2], r2.p) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [], "E": -1},
        {
          "S": -1,
          "M": [{"x": 400, "y": 500}, {"x": 500, "y": 500}, {"x": 500, "y": 550}],
          "E": 2
        }
      ],
      "junctions": [{"x": 400, "y": 286}],
      "renderStyle": "standard"
    }
  }
  connect(r2.n, load.p) {
    "Dyad": {
      "edges": [
        {
          "S": 1,
          "M": [
            {"x": 750, "y": 550},
            {"x": 750, "y": 690},
            {"x": 140, "y": 690},
            {"x": 140, "y": 406}
          ],
          "E": 2
        }
      ],
      "renderStyle": "standard"
    }
  }
  connect(load.n, gnd2.g, transformer.n[2]) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [], "E": -1},
        {"S": -1, "M": [{"x": 335, "y": 433.5}, {"x": 340, "y": 433.5}], "E": 2},
        {"S": -1, "M": [{"x": 600, "y": 406}, {"x": 600, "y": 286}], "E": 3}
      ],
      "junctions": [{"x": 335, "y": 406}],
      "renderStyle": "standard"
    }
  }
metadata {
  "Dyad": {
    "tests": {
      "case1": {
        "stop": 5,
        "expect": {
          "signals": ["transformer.i[1]", "transformer.i[2]", "transformer.v[1]", "transformer.v[2]"]
        }
      }
    }
  }
}
end
Flattened Source
dyad
"""
Test circuit for MTransformer with N=2.

Replicates the primary-side circuit from the MSL CompareTransformers example:
  SineVoltage → R1 → Transformer primary → Ground (primary side)
  Transformer secondary → R2 → Load → Ground (secondary side)

With these parameters the MTransformer(N=2) should behave identically
to the MSL Basic.Transformer(L1, L2, M).

Parameters (from MSL CompareTransformers):
  f = 10 Hz, Vpeak = 0.1 V, Vdc = 0.1 V, phase = pi/2
  n = 2 (turns ratio)
  R1 = 0.01 Ω, R2 = 0.0025 Ω, RL = 0.25 Ω
  L1 ≈ 0.16 H, L2 ≈ 0.04 H, M ≈ 0.07958 H
"""
test component MTransformer
  # Sine voltage source (primary side)
  sine = BlockComponents.Sources.Sine(offset = 0.1, amplitude = 0.1, frequency = 10.0, phase = 1.5707963267948966) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
      },
      "tags": []
    }
  }
  source = ElectricalComponents.Analog.Sources.VoltageSource() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 155, "y1": 125, "x2": 255, "y2": 225, "rot": 0}
      },
      "tags": []
    }
  }
  # Primary winding resistance
  r1 = ElectricalComponents.Analog.Basic.Resistor(R = 0.01) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 285, "y1": 236, "x2": 385, "y2": 336, "rot": 0}
      },
      "tags": []
    }
  }
  # The MTransformer with N=2
  transformer = ElectricalComponents.Analog.Basic.MTransformer(N = 2, L = [0.15996, 0.07958, 0.03999]) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 414, "y1": 236, "x2": 514, "y2": 336, "rot": 0}
      },
      "tags": []
    }
  }
  # Secondary winding resistance
  r2 = ElectricalComponents.Analog.Basic.Resistor(R = 0.0025) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 630, "y1": 500, "x2": 730, "y2": 600, "rot": 0}
      },
      "tags": []
    }
  }
  # Load resistor
  load = ElectricalComponents.Analog.Basic.Resistor(R = 0.25) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 155, "y1": 356, "x2": 255, "y2": 456, "rot": 0}
      },
      "tags": []
    }
  }
  # Separate grounds for primary and secondary (galvanic isolation)
  gnd1 = ElectricalComponents.Analog.Basic.Ground() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 620, "y1": 350, "x2": 720, "y2": 450, "rot": 0}
      },
      "tags": []
    }
  }
  gnd2 = ElectricalComponents.Analog.Basic.Ground() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 290, "y1": 500, "x2": 390, "y2": 600, "rot": 0}
      },
      "tags": []
    }
  }
relations
  # Drive voltage source with sine
  connect(sine.y, source.V) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 205, "y": 70}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  # Primary side: source → R1 → transformer winding 1 → ground
  connect(source.p, r1.p) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 140, "y": 175}, {"x": 140, "y": 286}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(r1.n, transformer.p[1]) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(transformer.n[1], gnd1.g, source.n) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [], "E": -1},
        {"S": -1, "M": [{"x": 670, "y": 286}], "E": 2},
        {
          "S": -1,
          "M": [{"x": 540, "y": 246}, {"x": 600, "y": 246}, {"x": 600, "y": 175}],
          "E": 3
        }
      ],
      "junctions": [{"x": 540, "y": 286}],
      "renderStyle": "standard"
    }
  }
  # Secondary side: transformer winding 2 → R2 → load → ground
  connect(transformer.p[2], r2.p) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [], "E": -1},
        {
          "S": -1,
          "M": [{"x": 400, "y": 500}, {"x": 500, "y": 500}, {"x": 500, "y": 550}],
          "E": 2
        }
      ],
      "junctions": [{"x": 400, "y": 286}],
      "renderStyle": "standard"
    }
  }
  connect(r2.n, load.p) {
    "Dyad": {
      "edges": [
        {
          "S": 1,
          "M": [
            {"x": 750, "y": 550},
            {"x": 750, "y": 690},
            {"x": 140, "y": 690},
            {"x": 140, "y": 406}
          ],
          "E": 2
        }
      ],
      "renderStyle": "standard"
    }
  }
  connect(load.n, gnd2.g, transformer.n[2]) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [], "E": -1},
        {"S": -1, "M": [{"x": 335, "y": 433.5}, {"x": 340, "y": 433.5}], "E": 2},
        {"S": -1, "M": [{"x": 600, "y": 406}, {"x": 600, "y": 286}], "E": 3}
      ],
      "junctions": [{"x": 335, "y": 406}],
      "renderStyle": "standard"
    }
  }
metadata {
  "Dyad": {
    "tests": {
      "case1": {
        "stop": 5,
        "expect": {
          "signals": ["transformer.i[1]", "transformer.i[2]", "transformer.v[1]", "transformer.v[2]"]
        }
      }
    }
  }
}
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.MTransformer()
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 = 5e+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.transformer.i[1]])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "ElectricalComponents.Analog.Basic.Tests.MTransformer_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.transformer.i[1]], width=2, label="Actual value of transformer.i[1]")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of transformer.i[1]")
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.transformer.i[2]])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "ElectricalComponents.Analog.Basic.Tests.MTransformer_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.transformer.i[2]], width=2, label="Actual value of transformer.i[2]")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of transformer.i[2]")
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.transformer.v[1]])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "ElectricalComponents.Analog.Basic.Tests.MTransformer_case1_sig2.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.transformer.v[1]], width=2, label="Actual value of transformer.v[1]")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of transformer.v[1]")
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.transformer.v[2]])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "ElectricalComponents.Analog.Basic.Tests.MTransformer_case1_sig3.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.transformer.v[2]], width=2, label="Actual value of transformer.v[2]")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of transformer.v[2]")
end
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>