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

Analog.Basic.Tests.VariableConductor

Test circuit for VariableConductor.

Same topology as ShowVariableResistor: a sine voltage source drives two parallel branches — a fixed 3-resistor chain (R1-R2-R3, each 1 Ohm) and a chain with two 1 Ohm resistors flanking a VariableConductor. The conductance ramps from 0.5 S (≡ 2 Ω) down to ~0.143 S (≡ 7 Ω) over 2 s, mirroring the resistance ramp in ShowVariableResistor.

Usage

ElectricalComponents.Analog.Basic.Tests.VariableConductor()

Behavior

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

Source

dyad
"""
Test circuit for VariableConductor.

Same topology as ShowVariableResistor: a sine voltage source drives
two parallel branches — a fixed 3-resistor chain (R1-R2-R3, each
1 Ohm) and a chain with two 1 Ohm resistors flanking a
VariableConductor. The conductance ramps from 0.5 S (≡ 2 Ω) down
to ~0.143 S (≡ 7 Ω) over 2 s, mirroring the resistance ramp in
ShowVariableResistor.
"""
test component VariableConductor
  "Sine voltage source: V=1 V, f=1 Hz"
  sine = BlockComponents.Sources.Sine(amplitude = 1, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
      },
      "tags": []
    }
  }
  "Voltage source driven by the sine signal"
  voltage_source = ElectricalComponents.Analog.Sources.VoltageSource() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 100, "y1": 230, "x2": 200, "y2": 330, "rot": 0}
      },
      "tags": []
    }
  }
  "Ground at source positive terminal"
  ground1 = ElectricalComponents.Analog.Basic.Ground() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 10, "y1": 660, "x2": 110, "y2": 760, "rot": 0}
      },
      "tags": []
    }
  }
  "Ground at return path (right side)"
  ground2 = ElectricalComponents.Analog.Basic.Ground() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 900, "y1": 690, "x2": 1000, "y2": 790, "rot": 0}
      },
      "tags": []
    }
  }
  "Fixed resistor chain (top branch)"
  R1 = ElectricalComponents.Analog.Basic.Resistor(R = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 300, "y1": 230, "x2": 400, "y2": 330, "rot": 0}
      },
      "tags": []
    }
  }
  R2 = ElectricalComponents.Analog.Basic.Resistor(R = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 500, "y1": 230, "x2": 600, "y2": 330, "rot": 0}
      },
      "tags": []
    }
  }
  R3 = ElectricalComponents.Analog.Basic.Resistor(R = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 700, "y1": 230, "x2": 800, "y2": 330, "rot": 0}
      },
      "tags": []
    }
  }
  "Variable conductor chain (bottom branch)"
  R4 = ElectricalComponents.Analog.Basic.Resistor(R = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 290, "y1": 530, "x2": 390, "y2": 630, "rot": 0}
      },
      "tags": []
    }
  }
  variable_conductor = ElectricalComponents.Analog.Basic.VariableConductor() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 500, "y1": 530, "x2": 600, "y2": 630, "rot": 0}
      },
      "tags": []
    }
  }
  R5 = ElectricalComponents.Analog.Basic.Resistor(R = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 700, "y1": 530, "x2": 800, "y2": 630, "rot": 0}
      },
      "tags": []
    }
  }
  "Ramp signal for variable conductance: 0.5 S down to ~0.143 S over 2 s"
  ramp = BlockComponents.Sources.Ramp(height = -0.357, offset = 0.5, duration = 2) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 380, "y1": 370, "x2": 480, "y2": 470, "rot": 0}
      },
      "tags": []
    }
  }
  "Fixed temperature to ground the thermal port (alpha=0, so T is irrelevant)"
  fixed_temp = ThermalComponents.Sources.FixedTemperature(T = 300.15) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 320, "y1": 740, "x2": 420, "y2": 840, "rot": 0}
      },
      "tags": []
    }
  }
relations
  guess variable_conductor.i = 0
  "Signal to voltage source"
  connect(sine.y, voltage_source.V) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 150, "y": 70}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  "Source positive to ground1"
  connect(voltage_source.p, ground1.g) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 60, "y": 280}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  "Source negative to both branches"
  connect(voltage_source.n, R1.p) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(voltage_source.n, R4.p) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 270, "y": 280}, {"x": 270, "y": 580}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  "Top branch: R1 -> R2 -> R3 -> ground2"
  connect(R1.n, R2.p) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(R2.n, R3.p) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(R3.n, ground2.g) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 950, "y": 280}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  "Bottom branch: R4 -> VC -> R5 -> ground2"
  connect(R4.n, variable_conductor.p) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(R5.n, ground2.g) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 950, "y": 580}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(variable_conductor.n, R5.p) {"Dyad": {"renderStyle": "standard", "edges": [{"S": 1, "E": 2, "M": []}]}}
  connect(ramp.y, variable_conductor.G) {
    "Dyad": {
      "renderStyle": "standard",
      "edges": [{"S": 1, "M": [{"x": 550, "y": 420}], "E": 2}]
    }
  }
  connect(fixed_temp.port, variable_conductor.heat_port) {
    "Dyad": {
      "renderStyle": "standard",
      "edges": [{"S": 1, "M": [{"x": 551, "y": 790}], "E": 2}]
    }
  }
metadata {
  "Dyad": {
    "tests": {
      "case1": {
        "stop": 1,
        "expect": {"signals": ["variable_conductor.i", "R2.i", "variable_conductor.LossPower"]}
      }
    }
  }
}
end
Flattened Source
dyad
"""
Test circuit for VariableConductor.

Same topology as ShowVariableResistor: a sine voltage source drives
two parallel branches — a fixed 3-resistor chain (R1-R2-R3, each
1 Ohm) and a chain with two 1 Ohm resistors flanking a
VariableConductor. The conductance ramps from 0.5 S (≡ 2 Ω) down
to ~0.143 S (≡ 7 Ω) over 2 s, mirroring the resistance ramp in
ShowVariableResistor.
"""
test component VariableConductor
  "Sine voltage source: V=1 V, f=1 Hz"
  sine = BlockComponents.Sources.Sine(amplitude = 1, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
      },
      "tags": []
    }
  }
  "Voltage source driven by the sine signal"
  voltage_source = ElectricalComponents.Analog.Sources.VoltageSource() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 100, "y1": 230, "x2": 200, "y2": 330, "rot": 0}
      },
      "tags": []
    }
  }
  "Ground at source positive terminal"
  ground1 = ElectricalComponents.Analog.Basic.Ground() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 10, "y1": 660, "x2": 110, "y2": 760, "rot": 0}
      },
      "tags": []
    }
  }
  "Ground at return path (right side)"
  ground2 = ElectricalComponents.Analog.Basic.Ground() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 900, "y1": 690, "x2": 1000, "y2": 790, "rot": 0}
      },
      "tags": []
    }
  }
  "Fixed resistor chain (top branch)"
  R1 = ElectricalComponents.Analog.Basic.Resistor(R = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 300, "y1": 230, "x2": 400, "y2": 330, "rot": 0}
      },
      "tags": []
    }
  }
  R2 = ElectricalComponents.Analog.Basic.Resistor(R = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 500, "y1": 230, "x2": 600, "y2": 330, "rot": 0}
      },
      "tags": []
    }
  }
  R3 = ElectricalComponents.Analog.Basic.Resistor(R = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 700, "y1": 230, "x2": 800, "y2": 330, "rot": 0}
      },
      "tags": []
    }
  }
  "Variable conductor chain (bottom branch)"
  R4 = ElectricalComponents.Analog.Basic.Resistor(R = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 290, "y1": 530, "x2": 390, "y2": 630, "rot": 0}
      },
      "tags": []
    }
  }
  variable_conductor = ElectricalComponents.Analog.Basic.VariableConductor() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 500, "y1": 530, "x2": 600, "y2": 630, "rot": 0}
      },
      "tags": []
    }
  }
  R5 = ElectricalComponents.Analog.Basic.Resistor(R = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 700, "y1": 530, "x2": 800, "y2": 630, "rot": 0}
      },
      "tags": []
    }
  }
  "Ramp signal for variable conductance: 0.5 S down to ~0.143 S over 2 s"
  ramp = BlockComponents.Sources.Ramp(height = -0.357, offset = 0.5, duration = 2) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 380, "y1": 370, "x2": 480, "y2": 470, "rot": 0}
      },
      "tags": []
    }
  }
  "Fixed temperature to ground the thermal port (alpha=0, so T is irrelevant)"
  fixed_temp = ThermalComponents.Sources.FixedTemperature(T = 300.15) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 320, "y1": 740, "x2": 420, "y2": 840, "rot": 0}
      },
      "tags": []
    }
  }
relations
  guess variable_conductor.i = 0
  "Signal to voltage source"
  connect(sine.y, voltage_source.V) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 150, "y": 70}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  "Source positive to ground1"
  connect(voltage_source.p, ground1.g) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 60, "y": 280}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  "Source negative to both branches"
  connect(voltage_source.n, R1.p) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(voltage_source.n, R4.p) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 270, "y": 280}, {"x": 270, "y": 580}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  "Top branch: R1 -> R2 -> R3 -> ground2"
  connect(R1.n, R2.p) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(R2.n, R3.p) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(R3.n, ground2.g) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 950, "y": 280}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  "Bottom branch: R4 -> VC -> R5 -> ground2"
  connect(R4.n, variable_conductor.p) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(R5.n, ground2.g) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 950, "y": 580}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(variable_conductor.n, R5.p) {"Dyad": {"renderStyle": "standard", "edges": [{"S": 1, "E": 2, "M": []}]}}
  connect(ramp.y, variable_conductor.G) {
    "Dyad": {
      "renderStyle": "standard",
      "edges": [{"S": 1, "M": [{"x": 550, "y": 420}], "E": 2}]
    }
  }
  connect(fixed_temp.port, variable_conductor.heat_port) {
    "Dyad": {
      "renderStyle": "standard",
      "edges": [{"S": 1, "M": [{"x": 551, "y": 790}], "E": 2}]
    }
  }
metadata {
  "Dyad": {
    "tests": {
      "case1": {
        "stop": 1,
        "expect": {"signals": ["variable_conductor.i", "R2.i", "variable_conductor.LossPower"]}
      }
    }
  }
}
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.VariableConductor()
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.variable_conductor.i])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "ElectricalComponents.Analog.Basic.Tests.VariableConductor_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.variable_conductor.i], width=2, label="Actual value of variable_conductor.i")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of variable_conductor.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.R2.i])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "ElectricalComponents.Analog.Basic.Tests.VariableConductor_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.R2.i], width=2, label="Actual value of R2.i")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of R2.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.variable_conductor.LossPower])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "ElectricalComponents.Analog.Basic.Tests.VariableConductor_case1_sig2.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.variable_conductor.LossPower], width=2, label="Actual value of variable_conductor.LossPower")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of variable_conductor.LossPower")
end
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>