* Native charge-conserving voltage-dependent capacitor -- self-test.
*
* GENERIC (non-PDK) coefficients.  A smooth sine is applied to two caps that
* implement the SAME law C(V) = C0*(1 + vc1*V + vc2*V^2):
*   Cn : the native charge-conserving device (vc1/vc2 on a .model)
*   Cb : the behavioral C='f(v)' form (ngspice expands it into a differentiator)
* and their terminal currents are compared.  The native device stores the exact
* charge Q(V) = C0*(V + vc1*V^2/2 + vc2*V^3/3) and stamps dQ/dV -- no auxiliary
* nodes, no differentiator.  It agrees with the behavioral form to integration-
* tolerance level; the native device is the exact, charge-conserving one.
*
* Expect: max|In - Ib| / peak ~ 1e-4 (differentiator realization error), and
* zero current at the sine peaks (dV/dt = 0), as a capacitor must have.
.param C0=1p vc1=0.02 vc2=-0.005
Vt a 0 SIN(0 1.8 1e6)

* native charge-conserving device
.model cn C tnom=25 vc1='vc1' vc2='vc2'
Cn  a n1  cn 'C0'
Vmn n1 0 dc 0

* behavioral C=f(v) (differentiator expansion)
Cb  a nb  'C0*(1+vc1*v(a,nb)+vc2*v(a,nb)*v(a,nb))'
Vmb nb 0 dc 0

.tran 1n 3u
.control
run
let d = abs(i(vmn)-i(vmb))
print vecmax(d) vecmax(abs(i(vmn)))
.endc
.end
