---
url: /dyad/dev/stdlib/ElectricalComponents/tests/Analog.Basic.Tests.VariableCapacitor.md
description: >-
  Dyad Analog.Basic.Tests.VariableCapacitor docs.  Test circuit for
  VariableCapacitor.
---

# `Analog.Basic.Tests.VariableCapacitor` {#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 {#Usage}

`ElectricalComponents.Analog.Basic.Tests.VariableCapacitor()`

## Behavior {#Behavior}

$$ \begin{align}
0 &= \mathrm{connect}\left( sine\_{+}y, vs\_{+}V \right) \\
0 &= \mathrm{connect}\left( vs\_{+}p, R1\_{+}p \right) \\
0 &= \mathrm{connect}\left( R1\_{+}n, vc\_{+}p \right) \\
0 &= \mathrm{connect}\left( vc\_{+}n, ground\_{+}g \right) \\
0 &= \mathrm{connect}\left( vs\_{+}n, ground\_{+}g \right) \\
0 &= \mathrm{connect}\left( ramp\_{+}y, vc\_{+}C \right) \\
\mathtt{sine.y}\left( t \right) &= ifelse\left( \mathtt{sine.continuous}, ifelse\left( t \geq \mathtt{sine.start_time}, \sin\left( \mathtt{sine.phase} + 6.2832 ~ \mathtt{sine.frequency} ~ \left(  - \mathtt{sine.shift_time} + t \right) \right), \sin\left( \mathtt{sine.phase} + 6.2832 ~ \mathtt{sine.frequency} ~ \left(  - \mathtt{sine.shift_time} + \mathtt{sine.start_time} \right) \right) \right) ~ \mathtt{sine.amplitude}, ifelse\left( t \geq \mathtt{sine.start_time}, \sin\left( \mathtt{sine.phase} + 6.2832 ~ \mathtt{sine.frequency} ~ \left(  - \mathtt{sine.shift_time} + t \right) \right) ~ \mathtt{sine.amplitude}, 0 \right) \right) + \mathtt{sine.offset} \\
\mathtt{vs.v}\left( t \right) &=  - \mathtt{vs.n.v}\left( t \right) + \mathtt{vs.p.v}\left( t \right) \\
\mathtt{vs.i}\left( t \right) &= \mathtt{vs.p.i}\left( t \right) \\
\mathtt{vs.n.i}\left( t \right) + \mathtt{vs.p.i}\left( t \right) &= 0 \\
\mathtt{vs.v}\left( t \right) &= \mathtt{vs.V}\left( t \right) ~ \mathtt{vs.uV} \\
\mathtt{R1.v}\left( t \right) &=  - \mathtt{R1.n.v}\left( t \right) + \mathtt{R1.p.v}\left( t \right) \\
\mathtt{R1.i}\left( t \right) &= \mathtt{R1.p.i}\left( t \right) \\
\mathtt{R1.n.i}\left( t \right) + \mathtt{R1.p.i}\left( t \right) &= 0 \\
\mathtt{R1.v}\left( t \right) &= \mathtt{R1.R} ~ \mathtt{R1.i}\left( t \right) \\
\mathtt{vc.v}\left( t \right) &=  - \mathtt{vc.n.v}\left( t \right) + \mathtt{vc.p.v}\left( t \right) \\
\mathtt{vc.i}\left( t \right) &= \mathtt{vc.p.i}\left( t \right) \\
\mathtt{vc.n.i}\left( t \right) + \mathtt{vc.p.i}\left( t \right) &= 0 \\
\mathtt{vc.Q}\left( t \right) &= max\left( \mathtt{vc.C}\left( t \right), \mathtt{vc.Cmin} \right) ~ \mathtt{vc.v}\left( t \right) \\
\mathtt{vc.i}\left( t \right) &= \frac{\mathrm{d}\mathtt{vc.Q}\left( t \right)}{\mathrm{d}t} \\
\mathtt{ramp.y}\left( t \right) &= ifelse\left( \mathtt{ramp.start_time} < t, ifelse\left( t < \mathtt{ramp.duration} + \mathtt{ramp.start_time}, \mathtt{ramp.offset} + \frac{\mathtt{ramp.height} ~ \left(  - \mathtt{ramp.start_time} + t \right)}{\mathtt{ramp.duration}}, \mathtt{ramp.height} + \mathtt{ramp.offset} \right), \mathtt{ramp.offset} \right) \\
\mathtt{ground.g.v}\left( t \right) &= 0
\end{align}
$$

## Source {#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": 420, "y1": 170, "x2": 320, "y2": 270, "rot": 0}
      },
      "tags": []
    }
  }
  "Voltage source driven by the sine signal"
  vs = ElectricalComponents.Analog.Sources.VoltageSource() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 320, "y1": 310, "x2": 220, "y2": 410, "rot": 0}
      },
      "tags": []
    }
  }
  "Series resistor (10 Ω)"
  R1 = ElectricalComponents.Analog.Basic.Resistor(R = 10) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 450, "y1": 310, "x2": 550, "y2": 410, "rot": 0}
      },
      "tags": []
    }
  }
  "Variable capacitor under test"
  vc = ElectricalComponents.Analog.Basic.VariableCapacitor() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 690, "y1": 310, "x2": 790, "y2": 410, "rot": 0}
      },
      "tags": []
    }
  }
  "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": 530, "y1": 170, "x2": 630, "y2": 270, "rot": 0}
      },
      "tags": []
    }
  }
  "Ground reference"
  ground = ElectricalComponents.Analog.Basic.Ground() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 450, "y1": 500, "x2": 550, "y2": 600, "rot": 0}
      },
      "tags": []
    }
  }
relations
  initial vc.v = 0
  connect(sine.y, vs.V) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 270, "y": 220}], "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": 870, "y": 360}, {"x": 870, "y": 450}, {"x": 500, "y": 450}],
          "E": 2
        }
      ],
      "renderStyle": "standard"
    }
  }
  connect(vs.n, ground.g) {
    "Dyad": {
      "edges": [
        {
          "S": 1,
          "M": [{"x": 120, "y": 360}, {"x": 120, "y": 450}, {"x": 500, "y": 450}],
          "E": 2
        }
      ],
      "renderStyle": "standard"
    }
  }
  connect(ramp.y, vc.C) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 740, "y": 220}], "E": 2}],
      "renderStyle": "standard"
    }
  }
metadata {
  "Dyad": {
    "tests": {"case1": {"stop": 1, "expect": {"signals": ["vc.v", "vc.i", "vc.Q"]}}}
  }
}
end
```

```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": 420, "y1": 170, "x2": 320, "y2": 270, "rot": 0}
      },
      "tags": []
    }
  }
  "Voltage source driven by the sine signal"
  vs = ElectricalComponents.Analog.Sources.VoltageSource() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 320, "y1": 310, "x2": 220, "y2": 410, "rot": 0}
      },
      "tags": []
    }
  }
  "Series resistor (10 Ω)"
  R1 = ElectricalComponents.Analog.Basic.Resistor(R = 10) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 450, "y1": 310, "x2": 550, "y2": 410, "rot": 0}
      },
      "tags": []
    }
  }
  "Variable capacitor under test"
  vc = ElectricalComponents.Analog.Basic.VariableCapacitor() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 690, "y1": 310, "x2": 790, "y2": 410, "rot": 0}
      },
      "tags": []
    }
  }
  "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": 530, "y1": 170, "x2": 630, "y2": 270, "rot": 0}
      },
      "tags": []
    }
  }
  "Ground reference"
  ground = ElectricalComponents.Analog.Basic.Ground() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 450, "y1": 500, "x2": 550, "y2": 600, "rot": 0}
      },
      "tags": []
    }
  }
relations
  initial vc.v = 0
  connect(sine.y, vs.V) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 270, "y": 220}], "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": 870, "y": 360}, {"x": 870, "y": 450}, {"x": 500, "y": 450}],
          "E": 2
        }
      ],
      "renderStyle": "standard"
    }
  }
  connect(vs.n, ground.g) {
    "Dyad": {
      "edges": [
        {
          "S": 1,
          "M": [{"x": 120, "y": 360}, {"x": 120, "y": 450}, {"x": 500, "y": 450}],
          "E": 2
        }
      ],
      "renderStyle": "standard"
    }
  }
  connect(ramp.y, vc.C) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 740, "y": 220}], "E": 2}],
      "renderStyle": "standard"
    }
  }
metadata {
  "Dyad": {
    "tests": {"case1": {"stop": 1, "expect": {"signals": ["vc.v", "vc.i", "vc.Q"]}}}
  }
}
end
```

## Test Cases {#Test-Cases}

### Test Case `case1` {#Test-Case-case1}

```julia
plt
```

![](voniwwa.svg)

```julia
plt
```

![](iwazjgn.svg)

```julia
plt
```

![](pgzipla.svg)

## Related {#Related}

* Examples

* Experiments

* Analyses

* Tests
  * [`Ground`](Analog.Basic.Tests.VariableCapacitor.md)

  * [`Ramp`](Analog.Basic.Tests.VariableCapacitor.md)

  * [`Resistor`](Analog.Basic.Tests.VariableCapacitor.md)

  * [`Sine`](Analog.Basic.Tests.VariableCapacitor.md)

  * [`VariableCapacitor`](Analog.Basic.Tests.VariableCapacitor.md)

  * [`VariableCapacitor`](Analog.Basic.Tests.VariableCapacitor.md)

  * [`VoltageSource`](Analog.Basic.Tests.VariableCapacitor.md)
