Electronic – Defining a nonlinear capacitor in Ngspice

simulationspice

I try to simulate an electronic circuit depicted in the schematic below.

enter image description here

This circuit contains two nonlinear devices :
These two devices are depending on the charge density and are defined as follows:

\$V_S = K \cdot Q^2 \$

\$I_d = k_{d1} \cdot exp(\frac{-Q^2}{k_{d2}})\$

where \$I = \frac{d_Q}{dt}\$

Is it possible to use the Behavioral Source?
According to the ngspice manual 25 , the nonlinear Source are only depending on the node voltages and branch current.
In my case the nonlinear Source or device depends on the integration of the branch current.
Is it possible to define a B-source which depends on the integration of the branch current ?

\$ K ,\ K_{d1} \ \text{and}\ k_{d2}\$ are constant values!

\$ I_d\$ is the flowing current through the Diode-like device and is defined as follows:

\$ I_d = k_{d1} exp(\frac{-Q^2}{k_{d2}}) \$

\$ V_s\$ is the dropping voltage across the nonlinear condensator-like device and is described by the following term:

\$ V_s = k \cdot Q^2 \$

Best Answer

As you say, the behavioral source in ngspice can only generate a voltage or current as a function of other node voltages or branch currents in your circuit. Nonetheless, you can use this, with some extra elements, to produce a nonlinear capacitor like you seem to need:

enter image description here

Here I used a linear CCCS and a linear capacitor to do the integration of the incoming current and track the charge variable. Then a nonlinear VCVS provides the capacitive behavior of your nonlinear capacitor. The gigohm resistor is there because SPICE requires every node to have a dc path to ground in order to obtain a solution; it will not affect the circuit solution significantly.

There's a similar example, using ngSpice syntax on page 89 in the ngSpice manual here.

Alternate solution

Also, be aware, ngSpice offers a behavioral capacitor model, described on page 71 in the manual I linked. The syntax is

CXXXXXXX n+ n C = ’expression’ <tc1 = value > < tc2 = value >

Here you should understand that the capacitance defined is the differential capacitance

\$C \equiv \frac{\mathrm{d}Q}{\mathrm{d}V}\$.

To put your equations in an appropriate form you need to re-work them a little bit. You have

\$V = K Q^2\$.

If you turn this around you have

\$Q = \sqrt{\frac{V}{K}}\$.

From which you can get

\$ \frac{\mathrm{d}Q}{\mathrm{d}V} = \frac{1}{2\sqrt{KV}}\$

Which you can easily implement in the ngSpice expression syntax.