-
Notifications
You must be signed in to change notification settings - Fork 23
User's guide
The SpicePy wiki provides the minimum information to use the code.
SpicePy makes use of a netlist to describe the circuit. The notation used is very similar to the SPICE one. A netlist is a text file (common extensions are .net or .cir).
In a netlist every component is defined as:
<label><X> <node+> <node-> <value> <other optional information>
where:
-
<label><X>is thelabelof the component andXis an identification number, -
<node+>is one node to which the component is connected. The sign+indicates that it is conventionally the positive node. It can be either a number or a letter, -
<node->is one node to which the component is connected. The sign-indicates that it is conventionally the negative node. It can be either a number or a letter, -
<value>is the value of the component, -
<other optional information>can be, for example, initial conditions for dynamic components of the phase of a generator to be used in AC problems
To give an example, a 5.6 ohm resistor caller R3 connected between nodes 1 and 5 is described in the netlist by:
R3 1 5 5.6
Every value used to define components and simulation parameters can be associated to a unit-prefix in the following list:
- (
'f = 1e-15','p = 1e-12','n = 1e-9','u = 1e-6','m = 1e-3','k = 1e3','meg = 1e6','g = 1e9','t = 1e-12')
To give an example, a 12.26 kohm resistor caller R5 connected between nodes A and B can be described in the netlist by:
R5 A B 12.26k
The analysis is defined using standard SPICE simulation commands. The simulation command have to be included at the end (last line) of the netlist. Right now SpicePy supports three kind of simulation:
- Operating point analysis: used to solve direct current (DC) networks and also to compute the initial conditions of circuits including dynamic components,
- AC analysis: used to solve AC network in frequency domain,
- Transient analysis: used to solve network in time domain.
To perform an operating point analysis it is sufficient to add at the end of the netlist the following simulation command:
.op
The AC analysis is defined by the following simulation command:
.ac type n f1 f2
where:
-
.acindicates the AC analysis -
typeindicates how to create the frequency range. It can be:-
linfor a linear sweep withnpoints between frequenciesf1andf2 (> f1) -
decfor a logarithmic sweep (by decades) withnpoints between frequenciesf1andf2 (> f1) -
octfor a logarithmic sweep (by octaves) withnpoints between frequenciesf1andf2 (> f1)
-
SpicePy supports:
-
single-frequency simulation using the following notation:
.ac lin 1 f f -
multi-frequency simulation using the general notation
.ac type n f1 f2
In the case of multi-frequency simulations, it is better to plot (rather than print) the results. Therefore, there is another command that must be included in the network to specify transfer functions. Defining a transfer function makes it possible to plot it bode diagram later (as shown in the examples).
.tf var1(<label><X>) var2(<label><X>) ... varN-1(<label><X>) varN(<label><X>)
where:
-
.tfindicates the intention to define a transfer function -
var1(<label><X>) var2(<label><X>)defines the transfer function:var1(<label><X>)/var2(<label><X>) -
varN-1(<label><X>) varN(<label><X>)defines the transfer function:varN-1(<label><X>)/varN(<label><X>)
It goes without saying that the number of variables after .tf must be even. Finally, variables can be either voltages (e.g. v(R1)) or current (e.g. i(L3)).
The transient analysis is defined by the following simulation command:
.tran step tend
where:
-
.tranindicates the transient analysis -
stepis the time step for numerical integration -
tendset the final integration time (i.e. the transient is analyzed in the range 0 - tend)
In transient analysis it is better to plot (rather than print) the results. Therefore, there is another command that must be included in the network to specify what to plot:
.plot v(<label><X>) ... i(<label><X>) ...
where:
-
.plotindicates the intention to plot something -
v(<label><X>)means the voltage across the component<label><X>(same notation as in the component description) -
i(<label><X>)means the current through the component<label><X>(same notation as in the component description)
The user can specify as many variable as desired and it not required any specific order. In the final plot voltages are grouped in the same plot and separated by currents which are grouped in another plot. Voltages and currents appear in the plot according to the order defined by the user in the .plot command.
Since all information about the circuit and the problem type are included in the netlist. The code to solve the circuit is always the same.
The following modules have to be imported.
import spicepy.netlist as ntl # module to read the netlist
from spicepy.netsolve import net_solve # module to solve the circuitGiven a netlist in the file circuit.net, the following code provides the circuit solution using the modified nodal analysis:
net = ntl.Network('circuit.net') # read the netlist
net_solve(net) # compute the circuit solutionBranch quantities as voltages, currents and powers can be calculated using the methods of the python class Network:
net.branch_voltage() # compute branch voltages
net.branch_current() # compute branch currents
net.branch_power() # compute branch powersBranch quantities can be eventually printed netprint module:
net.print()More details can be found in the examples page.