CircuitScript Demo

In [1]:
 
from circuit import *

Create the circuit object from the schematics file, set the simulation commands and simulate. By default, the Circuit object uses NG-Spice as its Spice backend, and a transient analysis for the voltage at node "out" is shown.

In [2]:
 
cir = Circuit(schematic="csdemo.sch")
cir.netlist.appendText(['.PRINT TRAN V(out)',
                        '.TRAN 1u 5m 0'])
cir.simulate()

Printing the results is a single-line instruction.

In [3]:
 
cir.simulationData.plot("time", "v(out)")

It's straight forward to perform calculations upon the results. The mathematical operations are defined as "DSP" objects. Just indicate what data the "DSP" object needs and pass the function such "DSP" object.

In [4]:
 
Vrms = cir.simulationData.doDSP(["time", "v(out)"], Rms()).result
print "%1.3f [V] RMS"%Vrms
0.989 [V] RMS

Perhaps CircuitScript becomes most useful when automating silumations for different parameter values:

In [5]:
 
pltlegend = ()
for R2 in [60, 80, 100, 120, 140]:
    pltlegend += ("R2=%d"%R2,)
    cir.netlist.changeValue("R2", R2)
    cir.simulate()
    Vrms = cir.simulationData.doDSP(["time", "v(out)"], Rms()).result
    cir.simulationData.plot("time", "v(out)")
    print "R2=%3d: %1.3f [V] RMS"%(R2, Vrms)
legend(pltlegend);
R2= 60: 0.748 [V] RMS
R2= 80: 0.881 [V] RMS
R2=100: 0.989 [V] RMS
R2=120: 1.078 [V] RMS
R2=140: 1.155 [V] RMS
In [ ]: