ngspicepy package¶
Module contents¶
ngspicepy¶
A python wrapper for ngspice.
ngspicepy as a python library for ngspice. It provides python wrappers for ngspice’s C API along with other useful functions. This allows one to run SPICE simulations and get the data as numpy as arrays directly from python instead of having to use files to use the data in python. Python has better tools to process data and plot relavent results. Thus ngspicepy gives a bridge between ngspice’s powerful simulator and python.
The library can be used in two ways.
- Directly using the functions provided by ngspicepy.
- Using the Netlist class.
-
ngspicepy.
send_command
(command)[source]¶ Send a command to ngspice.
The argument command is string that contains a valid ngspice command. See the chapter ‘Interactive Interpreter’ of the ngspice manual: http://ngspice.sourceforge.net/docs/ngspice26-manual.pdf
-
ngspicepy.
run_dc
(*args, **kwargs)[source]¶ Run a DC simulation on ngspice.
- Parameters:
*args
- A single string containing the source(s) followed by their start, stop and step values.
- src, start, stop, step[, src2, start, stop, step]
**kwargs
- The arguments specified as keyword arugments
src and src2 must be strings. start, stop and step can be either strings or floats. If they are strings, they must contain only a float and optionally one of ngspice’s scale factors and no spaces.
Examples:
>>> run_dc('v1 0 1 0.1') >>> run_dc('v2 0 1 1m v2 0 1 0.3') >>> run_dc('v1', 0, '1meg', '1k') >>> run_dc(src='v1', start=0, stop=1, step=0.1\\ src2='v2', start2=0, step2=0.3, stop2=1)
-
ngspicepy.
run_ac
(*args, **kwargs)[source]¶ Run an AC simulation on ngspice.
An AC simulation requires one to specify the start (fstart) and stop (fstop) frequecies, the type of variation (dec/oct/lin) and the number of points (npoints; per decade or octave if dec or oct are used)
- Parameters
*args
- A single string of the form ‘<variation> <npoints> <fstart> <fstop>’
*kwargs
- The arguements in variation, npoints, fstart or fstop specified as keyword arguments
- Examples:
>>> run_ac('dec 10 1 10') >>> run_ac('dec 10 1k 10meg') >>> run_ac('dec', 10, '1k', '100k') >>> run_ac(variation='dec', npoints=0, fstart=1, fstop=10)
-
ngspicepy.
run_tran
(*args, **kwargs)[source]¶ Run a TRAN simulation on ngspice.
- Parameters:
*args
- 1. A single string containing tstep, tstop, tstart, tmax and uic values. 2. The values of tmax and uic are optional. 3. tstep, tstop[, tstart, tmax, uic]
**kwargs
- The arguments in 2 specified as keyword arguments.
start, stop and step can be either strings or floats. If they are string, they must contain only a float and optionally one of the ngspice’s scale factor ans no spaces.
- Examples:
>>> run_tran('1 10 0 11 ') >>> run_tran('1ns 10ns 0 11ns') >>> run_tran('1ns', 0, '10ns', '11ns') >>> run_tran(tstep=1, tstop=10, tstart=0, tmax=11)
-
ngspicepy.
get_plot_names
()[source]¶ Return a list of plot names.
A plot is the name for a group of vectors.
- Example:
- A DC simulation run right after ngspice is loaded creates a plot called dc1 which contains the vectors generated by the DC simulation.
-
ngspicepy.
get_vector_names
(plot_name=None)[source]¶ Return a list of the names of the vectors in the given plot.
Parameter: plot_name : str specifies the plot whose vectors need to be returned. If it unspecified, the vector names from the current plot are returned.
-
ngspicepy.
get_data
(vector_arg, plot_arg=None)[source]¶ Get the data in a vector as a numpy array.
- Parameters:
- vector_arg
- denotes the vector name
- plot_agr
- denotes the plot name
-
ngspicepy.
get_all_data
(plot_name=None)[source]¶ Return a dictionary of all vectors in the specified plot.
- Parameter:
- plot_name
- denotes the plot name
-
ngspicepy.
set_options
(*args, **kwargs)[source]¶ Pass simulator options to ngspice.
- Parameters:
*args
- Options can be entered as a string
**kwargs
- Options can be entered as keyword arguments.
- Examples:
>>> set_options(trtol=1, temp=300) >>> set_options('trtol=1')
-
ngspicepy.
load_netlist
(netlist)[source]¶ Load ngspice with the specified netlist.
- Parameters:
- netlist : str
- The path to a file that contains the netlist.
- A list of strings where each string is one line of the netlist.
- A string containing the entire netlist with each line separated by a newline character.
The function does not check if the netlist is valid. An invalid netlist may cause ngspice to crash.
-
class
ngspicepy.
Netlist
(netlist)[source]¶ Bases:
object
A class that represents SPICE netlists.
-
get_plots
()[source]¶ Return a list of plot names.
A plot is the name for a group of vectors.
- Example:
- A DC simulation run right after ngspice is loaded creates a plot called dc1 which contains the vectors generated by the DC simulation.
-
get_vector
(vector_name, plot_name=None)[source]¶ Enable the user to get the data available in a given vector.
- Parameters:
- vector_arg
- It specifies name of the vector.
- plot_agr
- It specifies the name of the plot.
-
get_vector_names
(plot_name=None)[source]¶ Return a list of the names of the vectors in the given plot.
- Parameters:
- plot_name
- It specifies the plot whose vectors need to be returned. If it unspecified, the vector names from the current plot are returned.
-
get_vectors
(plot_name=None)[source]¶ Return a dictionary of all vectors in the specified plot.
- Parameter:
- plot_name
- It specifies the name of the plot.
-
run
()[source]¶ Run the simulation.
Depending on the arguments set in the set_simu() this function simply run that simulation.
-
setup_sim
(sim_type, *args, **kwargs)[source]¶ Set up the simulation.
- Parameters:
- sim_type
- The type of the simulation
*args
- The simulation parameters as arguments
**kwargs
- The simulation parameters as keyword arguments
- Examples:
>>> setup_sim('dc','v1 0 1 .3') >>> setup_sim('ac','dec 10 1 10') >>> setup_sim('tran','1 10')
-
-
ngspicepy.
clear_plots
(*args)[source]¶ Clear the specified plots names.
- Parameters:
*args
- Empty, which will clear all plots.
- Multiple arguments, each containing the name of a plot (a string).
- A string containing comma separated names of the plots that need to be deleted.
- A list or tuple of strings contianing the plots that need to be deleted.
- Examples:
>>> clear_plots() >>> clear_plots('dc dc2 dc3') >>> clear_plots(('dc1','dc2','dc3')) >>> clear_plots('dc1','dc2','dc3') >>> clear_plots(['dc1','dc2','dc3'])