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

Analog.Basic.Tests.VariableCapacitor

Test circuit for VariableCapacitor.

A sine voltage source (1 V, 1 Hz) drives an RC circuit where the capacitance ramps from 1e-3 F to 6e-3 F over 2 s. A 10 Ω series resistor provides the current path.

The charge-based formulation should produce smooth current despite the time-varying capacitance.

Usage

ElectricalComponents.Analog.Basic.Tests.VariableCapacitor()

Behavior

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

Source

dyad
"""
Test circuit for VariableCapacitor.

A sine voltage source (1 V, 1 Hz) drives an RC circuit where the
capacitance ramps from 1e-3 F to 6e-3 F over 2 s. A 10 Ω series
resistor provides the current path.

The charge-based formulation should produce smooth current despite
the time-varying capacitance.
"""
test component VariableCapacitor
  "Sine signal source: 1 V amplitude, 1 Hz"
  sine = BlockComponents.Sources.Sine(amplitude = 1, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 120, "x2": 120, "y2": 220, "rot": 0}
      }
    }
  }
  "Voltage source driven by the sine signal"
  vs = ElectricalComponents.Analog.Sources.VoltageSource() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 100, "y1": 300, "x2": 200, "y2": 400, "rot": 0}
      }
    }
  }
  "Series resistor (10 Ω)"
  R1 = ElectricalComponents.Analog.Basic.Resistor(R = 10) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 350, "y1": 300, "x2": 450, "y2": 400, "rot": 0}
      }
    }
  }
  "Variable capacitor under test"
  vc = ElectricalComponents.Analog.Basic.VariableCapacitor() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 580, "y1": 300, "x2": 680, "y2": 400, "rot": 270}
      }
    }
  }
  "Ramp for capacitance: 1e-3 F to 6e-3 F over 2 s"
  ramp = BlockComponents.Sources.Ramp(height = 5e-3, offset = 1e-3, duration = 2) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 500, "y1": 120, "x2": 600, "y2": 220, "rot": 0}
      }
    }
  }
  "Ground reference"
  ground = ElectricalComponents.Analog.Basic.Ground() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 380, "y1": 550, "x2": 480, "y2": 650, "rot": 0}
      }
    }
  }
relations
  initial vc.v = 0
  connect(sine.y, vs.V) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 150, "y": 170}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(vs.p, R1.p) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(R1.n, vc.p) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(vc.n, ground.g) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 630, "y": 600}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(vs.n, ground.g) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 150, "y": 600}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(ramp.y, vc.C) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 630, "y": 170}], "E": 2}],
      "renderStyle": "standard"
    }
  }
metadata {
  "Dyad": {
    "tests": {"case1": {"stop": 1, "expect": {"signals": ["vc.v", "vc.i", "vc.Q"]}}}
  }
}
end
Flattened Source
dyad
"""
Test circuit for VariableCapacitor.

A sine voltage source (1 V, 1 Hz) drives an RC circuit where the
capacitance ramps from 1e-3 F to 6e-3 F over 2 s. A 10 Ω series
resistor provides the current path.

The charge-based formulation should produce smooth current despite
the time-varying capacitance.
"""
test component VariableCapacitor
  "Sine signal source: 1 V amplitude, 1 Hz"
  sine = BlockComponents.Sources.Sine(amplitude = 1, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 120, "x2": 120, "y2": 220, "rot": 0}
      }
    }
  }
  "Voltage source driven by the sine signal"
  vs = ElectricalComponents.Analog.Sources.VoltageSource() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 100, "y1": 300, "x2": 200, "y2": 400, "rot": 0}
      }
    }
  }
  "Series resistor (10 Ω)"
  R1 = ElectricalComponents.Analog.Basic.Resistor(R = 10) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 350, "y1": 300, "x2": 450, "y2": 400, "rot": 0}
      }
    }
  }
  "Variable capacitor under test"
  vc = ElectricalComponents.Analog.Basic.VariableCapacitor() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 580, "y1": 300, "x2": 680, "y2": 400, "rot": 270}
      }
    }
  }
  "Ramp for capacitance: 1e-3 F to 6e-3 F over 2 s"
  ramp = BlockComponents.Sources.Ramp(height = 5e-3, offset = 1e-3, duration = 2) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 500, "y1": 120, "x2": 600, "y2": 220, "rot": 0}
      }
    }
  }
  "Ground reference"
  ground = ElectricalComponents.Analog.Basic.Ground() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 380, "y1": 550, "x2": 480, "y2": 650, "rot": 0}
      }
    }
  }
relations
  initial vc.v = 0
  connect(sine.y, vs.V) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 150, "y": 170}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(vs.p, R1.p) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(R1.n, vc.p) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(vc.n, ground.g) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 630, "y": 600}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(vs.n, ground.g) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 150, "y": 600}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(ramp.y, vc.C) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 630, "y": 170}], "E": 2}],
      "renderStyle": "standard"
    }
  }
metadata {
  "Dyad": {
    "tests": {"case1": {"stop": 1, "expect": {"signals": ["vc.v", "vc.i", "vc.Q"]}}}
  }
}
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.VariableCapacitor()
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.vc.v])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "ElectricalComponents.Analog.Basic.Tests.VariableCapacitor_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.vc.v], width=2, label="Actual value of vc.v")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of vc.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.vc.i])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "ElectricalComponents.Analog.Basic.Tests.VariableCapacitor_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.vc.i], width=2, label="Actual value of vc.i")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of vc.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.vc.Q])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "ElectricalComponents.Analog.Basic.Tests.VariableCapacitor_case1_sig2.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.vc.Q], width=2, label="Actual value of vc.Q")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of vc.Q")
end
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>