This example explains how to use the NgSpice binding.
import PySpice.Logging.Logging as Logging
logger = Logging.setup_logging()
from PySpice.Spice.NgSpice.Shared import NgSpiceShared
ngspice = NgSpiceShared.new_instance()
print(ngspice.exec_command('version -f'))
print(ngspice.exec_command('print all'))
print(ngspice.exec_command('devhelp'))
print(ngspice.exec_command('devhelp resistor'))
circuit = '''
.title Voltage Multiplier
.SUBCKT 1N4148 1 2
*
R1 1 2 5.827E+9
D1 1 2 1N4148
*
.MODEL 1N4148 D
+ IS = 4.352E-9
+ N = 1.906
+ BV = 110
+ IBV = 0.0001
+ RS = 0.6458
+ CJO = 7.048E-13
+ VJ = 0.869
+ M = 0.03
+ FC = 0.5
+ TT = 3.48E-9
.ENDS
Vinput in 0 DC 0V AC 1V SIN(0V 10V 50Hz 0s 0Hz)
C0 in 1 1mF
X0 1 0 1N4148
C1 0 2 1mF
X1 2 1 1N4148
C2 1 3 1mF
X2 3 2 1N4148
C3 2 4 1mF
X3 4 3 1N4148
C4 3 5 1mF
X4 5 4 1N4148
R1 5 6 1MegOhm
.options TEMP = 25°C
.options TNOM = 25°C
.options filetype = binary
.options NOINIT
.ic
.tran 0.0001s 0.4s 0s
.end
'''
ngspice.load_circuit(circuit)
print('Loaded circuit:')
print(ngspice.listing())
print(ngspice.show('c3'))
print(ngspice.showmod('c3'))
ngspice.run()
print('Plots:', ngspice.plot_names)
print(ngspice.ressource_usage())
print(ngspice.status())
plot = ngspice.plot(simulation=None, plot_name=ngspice.last_plot)
print(plot)
# ngspice.quit()
****** ** ngspice-33 : Circuit level simulation program ** The U. C. Berkeley CAD Group ** Copyright 1985-1994, Regents of the University of California. ** Copyright 2001-2020, The ngspice team. ** Please get your ngspice manual from http://ngspice.sourceforge.net/docs.html ** Please file your bug-reports at http://ngspice.sourceforge.net/bugrep.html ** Creation Date: Mon Oct 19 00:00:00 UTC 2020 ** ** CIDER 1.b1 (CODECS simulator) included ** XSPICE extensions included ** Relevant compilation options (refer to user's manual): ** Adms interface enabled ** OpenMP multithreading for BSIM3, BSIM4 enabled ** X11 interface not compiled into ngspice ** --enable-predictor ** ****** false = 0.000000e+00 true = 1.000000e+00 boltz = 1.380649e-23 c = 2.997925e+08 e = 2.718282e+00 echarge = 1.602177e-19 i = 0.000000e+00,1.000000e+00 kelvin = -2.73150e+02 no = 0.000000e+00 pi = 3.141593e+00 planck = 6.626070e-34 yes = 1.000000e+00 Devices available in the simulator URC : Uniform R.C. line ASRC : Arbitrary Source BJT : Bipolar Junction Transistor BSIM1 : Berkeley Short Channel IGFET Model BSIM2 : Berkeley Short Channel IGFET Model BSIM3 : Berkeley Short Channel IGFET Model Version-3 BSIM3v0 : Berkeley Short Channel IGFET Model Version-3 (3.0) BSIM3v1 : Berkeley Short Channel IGFET Model Version-3 (3.1) BSIM3v32 : Berkeley Short Channel IGFET Model Version-3 B4SOI : Berkeley SOI MOSFET model version 4.4.0 BSIM4 : Berkeley Short Channel IGFET Model-4 BSIM4v5 : Berkeley Short Channel IGFET Model-4 BSIM4v6 : Berkeley Short Channel IGFET Model-4 BSIM4v7 : Berkeley Short Channel IGFET Model-4 B3SOIPD : Berkeley SOI (PD) MOSFET model version 2.2.3 B3SOIFD : Berkeley SOI MOSFET (FD) model version 2.1 B3SOIDD : Berkeley SOI MOSFET (DD) model version 2.1 Capacitor : Fixed capacitor CCCS : Current controlled current source CCVS : Linear current controlled current source CplLines : Simple Coupled Multiconductor Lines CSwitch : Current controlled ideal switch Diode : Junction Diode model HFET1 : HFET1 Model HFET2 : HFET2 Model HiSIM2 : Hiroshima University STARC IGFET Model 2.8.0 HiSIMHV1 : Hiroshima University STARC IGFET Model - HiSIM_HV v.1 HiSIMHV2 : Hiroshima University STARC IGFET Model - HiSIM_HV v.2 Inductor : Fixed inductor mutual : Mutual inductors Isource : Independent current source JFET : Junction Field effect transistor JFET2 : Short channel field effect transistor LTRA : Lossy transmission line MES : GaAs MESFET model MESA : GaAs MESFET model Mos1 : Level 1 MOSfet model with Meyer capacitance model Mos2 : Level 2 MOSfet model with Meyer capacitance model Mos3 : Level 3 MOSfet model with Meyer capacitance model Mos6 : Level 6 MOSfet model with Meyer capacitance model Mos9 : Modified Level 3 MOSfet model Resistor : Simple linear resistor SOI3 : Basic Thick Film SOI3 model v2.7 Switch : Ideal voltage controlled switch Tranline : Lossless transmission line TransLine : Simple Lossy Transmission Line VBIC : Vertical Bipolar Inter-Company Model VCCS : Voltage controlled current source VCVS : Voltage controlled voltage source Vsource : Independent voltage source VDMOS : DMOS model based on Level 1 MOSFET model NBJT : 1D Numerical Bipolar Junction Transistor model NBJT2 : 2D Numerical Bipolar Junction Transistor model NUMD : 1D Numerical Junction Diode model NUMD2 : 2D Numerical Junction Diode model NUMOS : 2D Numerical MOS Field Effect Transistor model hicum0 : hicum0 created by adms hicum2 : hicum2 created by adms bjt504t : bjt504t created by adms ekv : ekv created by adms psp102 : psp102 created by adms spice2poly : 2G6 compatible polynomial controlled source climit : controlled limiter block divide : divider block d_dt : differentiator block gain : A simple gain block hyst : hysteresis block ilimit : current limiter block int : integrator block limit : limit block mult : multiplier block multi_input_pwl : multi_input_pwl block oneshot : one-shot pwl : piecwise linear controlled source sine : controlled sine wave oscillator slew : a simple slew rate follower block square : controlled square wave oscillator summer : summer block s_xfer : s-domain transfer function block triangle : controlled triangle wave oscillator filesource : File Source adc_bridge : analog-to-digital converter node bridge dac_bridge : digital-to-analog converter node bridge d_and : digital n-input and gate d_buffer : digital one-bit-wide buffer d_dff : digital d-type flip flop d_dlatch : digital d-type latch d_fdiv : digital frequency divider d_genlut : digital n-input x m-output look-up table gate d_inverter : digital one-bit-wide inverter d_jkff : digital jk-type flip flop d_lut : digital n-input look-up table gate d_nand : digital n-input nand gate d_nor : digital n-input nor gate d_open_c : digital one-bit-wide open-collector buffer d_open_e : digital one-bit-wide open-emitter buffer d_or : digital n-input or gate d_osc : controlled digital oscillator d_pulldown : digital pulldown resistor d_pullup : digital pullup resistor d_ram : digital random-access memory d_source : digital signal source d_srff : digital set-reset flip flop d_srlatch : digital sr-type latch d_state : digital state machine d_tff : digital toggle flip flop d_tristate : digital one-bit-wide tristate buffer d_xnor : digital n-input xnor gate d_xor : digital n-input xor gate aswitch : analog switch capacitor : Capacitor with voltage initial condition cmeter : ATESSE 1 compatible capacitance meter core : magnetic core inductor : Inductor with current initial condition lcouple : inductive coupling (for use with 'core' model) lmeter : ATESSE 1 compatible inductance meter potentiometer : potentiometer zener : zener diode memristor : Memristor interface sidiode : simple diode pswitch : analog PSPICE compatible switch d_to_real : Node bridge from digital to real with enable real_delay : A Z ** -1 block working on real data real_gain : A gain block for event-driven real data real_to_v : Node bridge from real to analog voltage table2d : 2D table model table3d : 3D table model Resistor - Simple linear resistor Model Parameters id# Name Dir Description 103 rsh inout Sheet resistance 106 narrow inout Narrowing of resistor 106 dw inout Narrowing of resistor 109 short inout Shortening of resistor 109 dlr inout Shortening of resistor 101 tc1 inout First order temp. coefficient 101 tc1r inout First order temp. coefficient 102 tc2 inout Second order temp. coefficient 102 tc2r inout Second order temp. coefficient 116 tce inout exponential temp. coefficient 104 defw inout Default device width 104 w inout Default device width 105 l inout Default device length 110 kf inout Flicker noise coefficient 111 af inout Flicker noise exponent 108 tnom inout Parameter measurement temperature 107 r inout Resistor model default value 107 res inout n.a. 112 bv_max inout maximum voltage over resistor 113 lf inout noise area length exponent 114 wf inout noise area width exponent 115 ef inout noise frequency exponent Instance Parameters id# Name Dir Description 1 resistance inout Resistance 1 r inout Resistance 10 ac inout AC resistance value 8 temp inout Instance operating temperature 14 dtemp inout Instance temperature difference with the rest of the circuit 3 l inout Length 2 w inout Width 12 m inout Multiplication factor 16 tc inout First order temp. coefficient 16 tc1 inout First order temp. coefficient 17 tc2 inout Second order temp. coefficient 19 tce inout exponential temp. coefficient 18 bv_max inout maximum voltage over resistor 13 scale inout Scale factor 15 noisy inout Resistor generate noise 15 noise inout n.a. 5 sens_resist in flag to request sensitivity WRT resistance 6 i out Current 7 p out Power 206 sens_dc out dc sensitivity 201 sens_real out dc sensitivity and real part of ac sensitivity 202 sens_imag out dc sensitivity and imag part of ac sensitivity 203 sens_mag out ac sensitivity of magnitude 204 sens_ph out ac sensitivity of phase 205 sens_cplx out ac sensitivity Loaded circuit: Voltage Multiplier 1 : voltage multiplier 2 : .global gnd 3 : .subckt 1n4148 1 2 5 : r1 1 2 5.827e+9 6 : d1 1 2 1n4148 8 : .model 1n4148 d is=4.352e-9 n=1.906 bv=110 ibv=0.0001 rs=0.6458 cjo=7.048e-13 vj=0.869 m=0.03 fc=0.5 tt=3.48e-9 9 : .ends 10 : vinput in 0 dc 0v ac 1v sin(0v 10v 50hz 0s 0hz) 11 : c0 in 1 1mf 12 : x0 1 0 1n4148 13 : c1 0 2 1mf 14 : x1 2 1 1n4148 15 : c2 1 3 1mf 16 : x2 3 2 1n4148 17 : c3 2 4 1mf 18 : x3 4 3 1n4148 19 : c4 3 5 1mf 20 : x4 5 4 1n4148 21 : r1 5 6 1megohm 26 : .ic 27 : .tran 0.0001s 0.4s 0s 22 : .options temp=25__c 23 : .options tnom=25__c 24 : .options filetype=binary 25 : .options noinit 29 : .end {'description': 'Capacitor: Fixed capacitor', 'Capacitor:': 'Fixed', 'device': 'c3', 'model': 'C', 'capacitance': 0.001, 'dtemp': 0, 'bv_max': 0} {'description': 'Capacitor models (Fixed capacitor)', 'Capacitor': 'models', 'model': 'C', 'cap': 0, 'cj': 0, 'cjsw': 0, 'defw': 0, 'defl': 0, 'narrow': 0, 'short': 0, 'del': 0, 'tc1': 0, 'tc2': 0, 'di': 0, 'thick': 0, 'bv_max': 0} Plots: ['tran1', 'const'] {'Total elapsed time (seconds)': 0.056, 'Total DRAM available': '7796.773 MB.', 'DRAM currently available': ' 828.523 MB.', 'Maximum ngspice program size': ' 865.227 MB.', 'Current ngspice program size': ' 71.234 MB.', 'Shared ngspice pages': ' 25.637 MB.', 'Text (code) pages': '4.000 kB.', 'Stack': '0 bytes.', 'Library pages': ' 231.688 MB.', 'Number of lines in the deck': 48, 'Netlist loading time': 0, 'Subckt and Param expansion time': 0, 'Netlist parsing time': 0, 'Nominal temperature': 25, 'Operating temperature': 25, 'Total iterations': 10571, 'Transient iterations': 10549, 'Circuit Equations': 14, 'Circuit original non-zeroes': 50, 'Circuit fill-in non-zeroes': 0, 'Circuit total non-zeroes': 50, 'Transient timepoints': 4040, 'Accepted timepoints': 4022, 'Rejected timepoints': 18, 'Total analysis time (seconds)': 0.041, 'Matrix load time': 0.013, 'Matrix synchronize time': 0, 'Matrix reorder time': 0, 'Matrix factor time': 0.001, 'Matrix solve time': 0.003, 'Transient analysis time': 0.041, 'Transient load time': 0.013, 'Transient sync time': 0, 'Transient factor time': 0.001, 'Transient solve time': 0.003, 'Transient trunc time': 0.002, 'Transient iterations for the last time point': 0, 'AC analysis time': 0, 'AC load time': 0, 'AC sync time': 0, 'AC factor time': 0, 'AC solve time': 0} {'vinput#branch': variable: vinput#branch current, 'V(6)': variable: V(6) voltage, 'V(5)': variable: V(5) voltage, 'V(4)': variable: V(4) voltage, 'V(3)': variable: V(3) voltage, 'V(2)': variable: V(2) voltage, 'V(1)': variable: V(1) voltage, 'in': variable: in voltage, 'time': variable: time time}