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

Analog.Basic.Tests.Potentiometer

Test circuit for Potentiometer.

A constant 10 V source is applied across pin_p and pin_n of a 100 Ω potentiometer. A 1 kΩ load resistor is connected from the contact (wiper) to ground. The wiper position ramps from 0 to 1 over 1 s.

At r=0 the contact is at pin_n (ground), so V_contact ≈ 0 V. At r=1 the contact is at pin_p, and V_contact = V_source * R_load / (Rp + R_load) where Rp → 0 as r → 1, so V_contact → 10 V.

A small capacitor across the load provides a differential state.

Usage

ElectricalComponents.Analog.Basic.Tests.Potentiometer()

Behavior

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

Source

dyad
"""
Test circuit for Potentiometer.

A constant 10 V source is applied across pin_p and pin_n of a 100 Ω
potentiometer. A 1 kΩ load resistor is connected from the contact
(wiper) to ground. The wiper position ramps from 0 to 1 over 1 s.

At r=0 the contact is at pin_n (ground), so V_contact ≈ 0 V.
At r=1 the contact is at pin_p, and V_contact = V_source * R_load / (Rp + R_load)
where Rp → 0 as r → 1, so V_contact → 10 V.

A small capacitor across the load provides a differential state.
"""
test component Potentiometer
  "Constant signal: 10 V"
  const_sig = BlockComponents.Sources.Constant(k = 10) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": -20, "y1": 380, "x2": 80, "y2": 480, "rot": 0}
      },
      "tags": []
    }
  }
  "Voltage source"
  vs = ElectricalComponents.Analog.Sources.VoltageSource() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 120, "y1": 480, "x2": 220, "y2": 380, "rot": 90}
      },
      "tags": []
    }
  }
  "Potentiometer under test (100 Ω)"
  pot = ElectricalComponents.Analog.Basic.Potentiometer(R = 100) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 450, "y1": 410, "x2": 550, "y2": 310, "rot": 0}
      },
      "tags": []
    }
  }
  "Load resistor (1 kΩ) on wiper"
  R_load = ElectricalComponents.Analog.Basic.Resistor(R = 1000) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 550, "y1": 500, "x2": 450, "y2": 600, "rot": 270}
      },
      "tags": []
    }
  }
  "Small capacitor for dynamics"
  cap = ElectricalComponents.Analog.Basic.Capacitor(C = 1e-6) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 740, "y1": 500, "x2": 640, "y2": 600, "rot": 270}
      },
      "tags": []
    }
  }
  "Ramp for wiper position: 0.01 to 0.99 over 1 s"
  ramp = BlockComponents.Sources.Ramp(height = 0.98, offset = 0.01, duration = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 190, "y1": 220, "x2": 290, "y2": 320, "rot": 0}
      },
      "tags": []
    }
  }
  "Fixed temperature for thermal port"
  fixed_temp = ThermalComponents.Sources.FixedTemperature(T = 293.15) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 450, "y1": 90, "x2": 550, "y2": 190, "rot": 90}
      },
      "tags": []
    }
  }
  "Ground reference"
  ground = ElectricalComponents.Analog.Basic.Ground() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 450, "y1": 840, "x2": 550, "y2": 940, "rot": 0}
      },
      "tags": []
    }
  }
relations
  initial cap.v = 0
  guess pot.pin_p.i = 0.1
  guess pot.pin_n.i = 0
  guess pot.contact.i = 0
  connect(const_sig.y, vs.V) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(vs.p, pot.pin_p) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 170, "y": 360}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(vs.n, ground.g) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 170, "y": 810}, {"x": 500, "y": 810}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(pot.pin_n, ground.g) {
    "Dyad": {
      "edges": [
        {
          "S": 1,
          "M": [{"x": 870, "y": 360}, {"x": 870, "y": 810}, {"x": 500, "y": 810}],
          "E": 2
        }
      ],
      "renderStyle": "standard"
    }
  }
  connect(R_load.n, cap.n, ground.g) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [{"x": 500, "y": 625}], "E": -1},
        {"S": -1, "M": [{"x": 690, "y": 625}], "E": 2},
        {"S": 3, "M": [{"x": 500, "y": 732.5}, {"x": 590, "y": 732.5}], "E": -1}
      ],
      "junctions": [{"x": 590, "y": 625}],
      "renderStyle": "standard"
    }
  }
  connect(ramp.y, pot.r) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 450, "y": 270}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(fixed_temp.port, pot.heat_port) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(pot.contact, R_load.p, cap.p) {
    "Dyad": {
      "edges": [
        {
          "S": 1,
          "M": [{"x": 550, "y": 260}, {"x": 640, "y": 260}, {"x": 640, "y": 450}],
          "E": -1
        },
        {"S": -1, "M": [{"x": 500, "y": 450}], "E": 2},
        {"S": 3, "M": [{"x": 690, "y": 450}], "E": -1}
      ],
      "junctions": [{"x": 550, "y": 450}],
      "renderStyle": "standard"
    }
  }
metadata {
  "Dyad": {
    "tests": {
      "case1": {
        "stop": 1,
        "expect": {"signals": ["pot.contact.v", "pot.Rp", "pot.Rn", "R_load.i"]}
      }
    }
  }
}
end
Flattened Source
dyad
"""
Test circuit for Potentiometer.

A constant 10 V source is applied across pin_p and pin_n of a 100 Ω
potentiometer. A 1 kΩ load resistor is connected from the contact
(wiper) to ground. The wiper position ramps from 0 to 1 over 1 s.

At r=0 the contact is at pin_n (ground), so V_contact ≈ 0 V.
At r=1 the contact is at pin_p, and V_contact = V_source * R_load / (Rp + R_load)
where Rp → 0 as r → 1, so V_contact → 10 V.

A small capacitor across the load provides a differential state.
"""
test component Potentiometer
  "Constant signal: 10 V"
  const_sig = BlockComponents.Sources.Constant(k = 10) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": -20, "y1": 380, "x2": 80, "y2": 480, "rot": 0}
      },
      "tags": []
    }
  }
  "Voltage source"
  vs = ElectricalComponents.Analog.Sources.VoltageSource() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 120, "y1": 480, "x2": 220, "y2": 380, "rot": 90}
      },
      "tags": []
    }
  }
  "Potentiometer under test (100 Ω)"
  pot = ElectricalComponents.Analog.Basic.Potentiometer(R = 100) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 450, "y1": 410, "x2": 550, "y2": 310, "rot": 0}
      },
      "tags": []
    }
  }
  "Load resistor (1 kΩ) on wiper"
  R_load = ElectricalComponents.Analog.Basic.Resistor(R = 1000) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 550, "y1": 500, "x2": 450, "y2": 600, "rot": 270}
      },
      "tags": []
    }
  }
  "Small capacitor for dynamics"
  cap = ElectricalComponents.Analog.Basic.Capacitor(C = 1e-6) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 740, "y1": 500, "x2": 640, "y2": 600, "rot": 270}
      },
      "tags": []
    }
  }
  "Ramp for wiper position: 0.01 to 0.99 over 1 s"
  ramp = BlockComponents.Sources.Ramp(height = 0.98, offset = 0.01, duration = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 190, "y1": 220, "x2": 290, "y2": 320, "rot": 0}
      },
      "tags": []
    }
  }
  "Fixed temperature for thermal port"
  fixed_temp = ThermalComponents.Sources.FixedTemperature(T = 293.15) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 450, "y1": 90, "x2": 550, "y2": 190, "rot": 90}
      },
      "tags": []
    }
  }
  "Ground reference"
  ground = ElectricalComponents.Analog.Basic.Ground() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 450, "y1": 840, "x2": 550, "y2": 940, "rot": 0}
      },
      "tags": []
    }
  }
relations
  initial cap.v = 0
  guess pot.pin_p.i = 0.1
  guess pot.pin_n.i = 0
  guess pot.contact.i = 0
  connect(const_sig.y, vs.V) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(vs.p, pot.pin_p) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 170, "y": 360}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(vs.n, ground.g) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 170, "y": 810}, {"x": 500, "y": 810}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(pot.pin_n, ground.g) {
    "Dyad": {
      "edges": [
        {
          "S": 1,
          "M": [{"x": 870, "y": 360}, {"x": 870, "y": 810}, {"x": 500, "y": 810}],
          "E": 2
        }
      ],
      "renderStyle": "standard"
    }
  }
  connect(R_load.n, cap.n, ground.g) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [{"x": 500, "y": 625}], "E": -1},
        {"S": -1, "M": [{"x": 690, "y": 625}], "E": 2},
        {"S": 3, "M": [{"x": 500, "y": 732.5}, {"x": 590, "y": 732.5}], "E": -1}
      ],
      "junctions": [{"x": 590, "y": 625}],
      "renderStyle": "standard"
    }
  }
  connect(ramp.y, pot.r) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 450, "y": 270}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(fixed_temp.port, pot.heat_port) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(pot.contact, R_load.p, cap.p) {
    "Dyad": {
      "edges": [
        {
          "S": 1,
          "M": [{"x": 550, "y": 260}, {"x": 640, "y": 260}, {"x": 640, "y": 450}],
          "E": -1
        },
        {"S": -1, "M": [{"x": 500, "y": 450}], "E": 2},
        {"S": 3, "M": [{"x": 690, "y": 450}], "E": -1}
      ],
      "junctions": [{"x": 550, "y": 450}],
      "renderStyle": "standard"
    }
  }
metadata {
  "Dyad": {
    "tests": {
      "case1": {
        "stop": 1,
        "expect": {"signals": ["pot.contact.v", "pot.Rp", "pot.Rn", "R_load.i"]}
      }
    }
  }
}
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.Potentiometer()
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.pot.contact.v])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "ElectricalComponents.Analog.Basic.Tests.Potentiometer_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.pot.contact.v], width=2, label="Actual value of pot.contact.v")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of pot.contact.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.pot.Rp])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "ElectricalComponents.Analog.Basic.Tests.Potentiometer_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.pot.Rp], width=2, label="Actual value of pot.Rp")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of pot.Rp")
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.pot.Rn])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "ElectricalComponents.Analog.Basic.Tests.Potentiometer_case1_sig2.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.pot.Rn], width=2, label="Actual value of pot.Rn")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of pot.Rn")
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.R_load.i])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "ElectricalComponents.Analog.Basic.Tests.Potentiometer_case1_sig3.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.R_load.i], width=2, label="Actual value of R_load.i")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of R_load.i")
end
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>