**Kernel:**Python 3 (old Anaconda 3)

# Symbolic modified nodal analysis

Last update: 12/9/2017

**Abstract:** The python code in this notebook will read in a spice like circuit netlist file and formulate a set of network equations in symbolic form using sympy. These equations can then be copied to a different notebook where the node voltages can be numerically solved using sympy or numpy. Linear resistors, capacitors, inductors, independent sources and controlled sources are supported.

**Introduction:** This node analysis code started as a translation from some C code to generate a nodal admittance matrix that I had written in 1988. I wrote this code for two reasons. Free versions of Spice for the PC didn't exist at the time and I wanted to use some of the code from the Numerical Recipes in C [1] Book. The original C code worked well and calculated numeric solutions. I then started writing some C code to generate the matrices with symbolic values and then intended to use LISP to symbolically solve the equations. I didn’t get too far with this effort. The LISP code would generate huge symbolic strings with no simplification. The output was a big pile of trash that was not in the least bit useful or decipherable.

In 2014, I started to use python for my little coding projects and engineering calculations. There are some nice python libraries for numeric and symbolic calculations (such as numpy and sympy), so I decided to try writing a python script to generate the node equations based on the old C code I had written many years before. Part way into this project I discovered that there is a new nodal analysis technique being taught today in engineering school called the modified nodal analysis [2][3]. My motivation for reviving this coding project is my continued interest in circuit analysis and synthesis.

**Description:** The modified nodal analysis provides an algorithmic method for generating systems of independent equations for linear circuit analysis. Some of my younger colleagues at work were taught this method, but I never heard of it until a short time ago. These days, I never really analyze a circuit by hand, unless it’s so simple that you can almost do it by inspection. Most problems that an electrical engineer encounters on the job are complex enough that they use computers to analyze the circuits. LTspice [4] is the version of spice that I use, since it’s free and does a good job converging when analyzing switching circuits.

My code started initially by following Erik Cheever's Analysis of Resistive Circuits, reference [5] MATLAB code, to generate modified nodal equations. I somewhat followed his MATLAB file for resistors, capacitors, opamps and independent sources. The naming of the matrices follows his convention. The preprocessor and parser code was converted from my old C code. The use of pandas for a data frame is new and sympy [6] is used to do the math and the use of element stamps is from reference [7].

Inductors are being addressed in the D matrix. Erik's code puts inductors into the G matrix as 1/s/L. My code puts the inductor contribution into the D matrix and the unknown current from the unductor into the B and C matricies. Coupled inductors also affect the D matrix, so it makes sense to allow the inductors to be in the D matrix rather than the G matrix.

**Network equations:** The network equations are a set of independent equations expressed in this code in matrix form. There is an equation for each node based on Kirchhoff's current law (KCL) [8] and an equation for each current unknown. The current unknowns are the currents from the voltages sources, op amps, voltage controlled voltage sources, current controlled voltage sources, current controlled current sources and inductors.

Equation 1 is the network equations in matrix form.

$A\cdot X = Z$

The A matrix describes the connectivity of the resistors, capacitors and G type (VCCS) circuit elements. The column vector X are the unknown node voltages and unknown currents terms from the voltage sources and inductors. The column vector Z is made of the known voltages and currents. The A is formed by four sub matrices, G, B, C and D, which are described below.

$A = \begin{bmatrix}G B\\C D\end{bmatrix}$

The matrix G is formed from the coefficients representing the KCL equations for each node. The positive diagonal of G$_{k,k}$ are the conductance terms of the resistor and capacitor elements connected to node k. The off diagonal terms of G$_{k,j}$ are the resistors and capacitor conductances connecting node k to node j. G type elements (VCCS) have input to the G matrix at the connection and controlling node positions.

The B matrix describes the connectivity of the unknown branch currents. Independent voltage sources, opamps, H, F and E type elements as well as inductors have inputs to the B matrix.

The C matrix describes the connectivity of the unknown branch currents and is mainly the transpose of B matrix, with the exception of the F type elements (CCCS) and includes the E type value.

The D matrix describes also connectivity of the unknown currents. The D matrix is composed of zeros unless there are controlled sources and inductors in the network.

The X vector is comprised of the V and J vectors as shown below. $X = \begin{bmatrix}V\\J\end{bmatrix}$ The V vector contains the node voltages which are the voltage unknowns to be solved for. The J vector contains the unknown currents from each voltage source.

The Z vector is comprised of the I and Ev vectors as shown below. $Z = \begin{bmatrix}I\\Ev\end{bmatrix}$ The I vector contains the known currents and the Ev vector contains the known voltages. Ev is used as the variable because sympy uses e and E sometimes for the constant e=2.71, sometimes called Euler's number [9]. The use of E or e as a symbol was causing some errors when the code was run.

Putting all the parts together:

$\begin{bmatrix}G B\\C D\end{bmatrix} \cdot \begin{bmatrix}V\\J\end{bmatrix} = \begin{bmatrix}I\\Ev\end{bmatrix}$

**Stamps:** Stamps are templates for modifying the B, C and D matrices and facilitate the construction of the matrices. The stamps used in this implementation of the MNA follow the stamps of reference [7].

**Code description:** The code is divided in the following sections.
Preprocessor: The preprocessor reads in the netlist text file and removes comments, extra spaces and blank lines. The first letter of the element type is capitalized to make subsequent parsing of the file easier. The number of lines are counted and the number of entries on each line are checked to make sure the count is consistent with the element type.

Parser: The parser code loads the preprocessed netlist into a data frame. A report is generated which consists of a count of the element types in the netlist.

Matrix formulation: Each of the matrices and vectors are generated.

Circuit equation generation: The circuit equations are generated in a for loop. Sympy automatically does some simplification according to its default settings. Two for loops perform the matrix multiplication on equation 1. The laplace variable s is used when inductors and capacitors are included in the circuit[7].

**Code validation:** The python code was verified by analyzing test circuits and comparing the results to LTspice. A collection of worked circuits can be found in reference [8]. See the revision history below for an indication of validation performed so far. Other test circuits can be found in reference [11]. Test vectors can be found in the github repository for this project. See the readme file in the test circuits folder for a discription of the validation tests. Code validation is on going at this time.

**Usage:** The input file is a text file called the net list. It can generated by using a text editor using the format listed below or by drawing the schematic and exporting the net list. LTspice can be used to draw the schematic of the circuit to be analyzed. The bit mapped image of the schematic can be copied and included in a document. The net list can be imported into the python code and the circuit equations can be generated in symbolic form. The User’s guide can be found on github repository.

**Change log:** The code development change log can be found on github here.

The backup history can also be found on github. Backups

**Survey of other symbolic circuit analysis code:** The python code presented in this notebook is somewhat unique since python is open source, free and runs on a variety of platforms. The code presented in this ipython notebook is portable. As described below, this code is made available under a public domain licence and archived in a github repository.

There are other symbolic circuit analysis codes available and some of these are described here. Some of these codes are based on commercial software such as MATLAB [12], TINA [13] and Maple [14].

SLiCAP is a symbolic linear analysis tool. SLiCAP runs in MATLAB.

TINA is an acronym of Toolkit for Interactive Network Analysis. The TINA design suite is a circuit simulator and PCB design software package for analyzing, designing, and real time testing of analog, digital, HDL, MCU, and mixed electronic circuits and their PCB layouts. TINA has some symbolic analysis capability.

Maple is a mathematical package and there is an application note available describing it use in symbolic circuit analysis. The application note presents an method for evaluating, solving and designing a common, but not so simple pulse-mode high-gain transimpedance amplifier or TIA circuit.

Symbolic Circuit Analysis is a web page devoted to symbolic circuit analysis.

SAPWIN is a windows program package for symbolic and numerical simulation of analog circuits.

Lcapy is an experimental Python package for teaching linear circuit analysis. It uses SymPy for symbolic mathematics.

**License:** This work (includes python code, documentation, test circuits, etc.) is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Share — copy and redistribute the material in any medium or format
Adapt — remix, transform, and build upon the material for any purpose, even commercially.
https://creativecommons.org/licenses/by-sa/4.0/

Numerical Recipes in C: The Art of Scientific Computing, William H. Press, Brian P. Flannery, Saul A. Teukolsky, William T. Vetterling, Cambridge University Press; 1988

The modified nodal approach to network analysis, Chung-Wen Ho, A. Ruehli, P. Brennan, IEEE Transactions on Circuits and Systems ( Volume: 22, Issue: 6, Jun 1975 )

Modified nodal analysis, wikipedia.org, retrieved October 6, 2017

LTspice, Linear Technology Corporation, retrieved October 6, 2017

Analysis of Resistive Circuits, retrieved October 6, 2017

Laplace transform, wikipedia.org, retrieved December 3, 2017

ECE 570 Session 3, Computer Aided Engineering for Integrated Circuits, http://www2.engr.arizona.edu/~ece570/session3.pdf

Kirchhoff's circuit laws, Wikipedia.com, retrieved October 8, 2017

e (mathematical constant), Wikipedia.com, retrieved October 8, 2017

Solved Problems, A Source of Free Solved Problems,Category Archives: Electrical Circuits, retrieved October 6, 2017

MATLAB, retrieved October 6, 2017

TINA, retrieved October 6, 2017

Maple, retrieved October 6, 2017

## Open net list and preprocess it

The following steps are performed:

file name extenstion is defaulted to .net

remove blank lines and comments

convert first letter of element name to upper case

removes extra spaces between entries

count number of entries on each line, make sure the count is correct, count each element type

## Parser

The parser performs the following operations.

puts branch elements into data frame

counts number of nodes

data frame lables:

element: type of element

p node: positive node

n node: negitive node, for a current source, the arrow point terminal, LTspice puts the inductor phasing dot on this terminal

cp node: controlling positive node of branch

cn node: controlling negitive node of branch

Vout: opamp output node

value: value of element or voltage

Vname: voltage source through which the controlling current flows. Need to add a zero volt voltage source to the controlling branch.

Lname1: name of coupled inductor 1

Lname2: name of coupled inductor 2

### Functions to load branch elements into data frame and check for gaps in node numbering

### Load circuit net list into the data frames

## Print net list report

element | p node | n node | cp node | cn node | Vout | value | Vname | Lname1 | Lname2 | |
---|---|---|---|---|---|---|---|---|---|---|

0 | V1 | 1 | 0 | NaN | NaN | NaN | 1 | NaN | NaN | NaN |

1 | L1 | 2 | 3 | NaN | NaN | NaN | 4.411 | NaN | NaN | NaN |

2 | L2 | 3 | 4 | NaN | NaN | NaN | 2.5346 | NaN | NaN | NaN |

3 | C1 | 2 | 0 | NaN | NaN | NaN | 0.6529 | NaN | NaN | NaN |

4 | C2 | 3 | 0 | NaN | NaN | NaN | 0.814 | NaN | NaN | NaN |

5 | R2 | 4 | 0 | NaN | NaN | NaN | 1 | NaN | NaN | NaN |

6 | R1 | 2 | 1 | NaN | NaN | NaN | 3 | NaN | NaN | NaN |

element | p node | n node | |
---|---|---|---|

0 | V1 | 1 | 0 |

1 | L1 | 2 | 3 |

2 | L2 | 3 | 4 |

#### some debugging notes:

Is is possible to have i_unk == 0 ?, what about a network with only current sources? This would make B = 0 for example. Did one test, need to run others Is there a valid op amp case where B is n by 1?

## G matrix

The G matrix is n by n, where n is the number of nodes. The matrix is formed by the interconnections between the resistors, capacitors and VCCS type elements. In the original paper G is called Yr, where Yr, is a reduced form of the nodal matrix excluding the contributions due to voltage sources, current controlling elements, etc. In python row and columns are: G[row, column]

## B Matrix

The B matrix is an n by m matrix with only 0, 1 and -1 elements, where n = number of nodes and m is the number of current unknowns, i_unk. There is one column for each unknown current. The code loop through all the branches and process elements that have stamps for the B matrix:

Voltage sources (V)

Opamps (O)

CCVS (H)

CCCS (F)

VCVS (E)

Inductors (L)

The order of the columns is as they appear in the netlist. CCCS (F) does not get its own column because the controlling current is through a zero volt voltage source, called Vname and is already in the net list.

## C matrix

The C matrix is an m by n matrix with only 0, 1 and -1 elements (except for controlled sources). The code is similar to the B matrix code, except the indices are swapped. The code loops through all the branches and process elements that have stamps for the C matrix:

Voltage sources (V)

Opamps (O)

CCVS (H)

CCCS (F)

VCVS (E)

Inductors (L)

References use in the debugging of the opamp stamp: Design of Analog Circuits Through Symbolic Analysis edited by Mourad Fakhfakh, Esteban Tlelo-Cuautle, Francisco V. Fernández Computer Aided Design and Design Automation edited by Wai-Kai Chen http://users.ecs.soton.ac.uk/mz/CctSim/chap1_4.htm

## D matrix

The D matrix is an m by m matrix, where m is the number of unknown currents.

m = i_unk = num_v+num_opamps+num_vcvs+num_ccvs+num_ind+num_cccs

Stamps that affect the D matrix are: inductor, ccvs and cccs inductors: minus sign added to keep current flow convention consistent

Coupled inductors notes: Can the K statement be anywhere in the net list, even before Lx and Ly? 12/6/2017 doing some debugging on with coupled inductors LTspice seems to put the phasing dot on the neg node when it generates the netlist This code uses M for mutual inductance, LTspice uses k for the coupling coefficient.

## V matrix

The V matrix is an n by 1 matrix formed of the node voltages, where n is the number of nodes. Each element in V corresponds to the voltage at the node.

Maybe make small v's v_1 so as not to confuse v1 with V1.

## J matrix

The J matrix is an m by 1 matrix, where m is the number of unknown currents.

i_unk = num_v+num_opamps+num_vcvs+num_ccvs+num_ind+num_cccs

## I matrix

The I matrix is an n by 1 matrix, where n is the number of nodes. The value of each element of I is determined by the sum of current sources into the corresponding node. If there are no current sources connected to the node, the value is zero.

## Ev matrix

The Ev matrix is mx1 and holds the values of the independent voltage sources.

## Z matrix

The Z matrix holds the independent voltage and current sources and is the combination of 2 smaller matrices I and Ev. The Z matrix is (m+n) by 1, n is the number of nodes, and m is the number of independent voltage sources. The I matrix is n by 1 and contains the sum of the currents through the passive elements into the corresponding node (either zero, or the sum of independent current sources). The Ev matrix is m by 1 and holds the values of the independent voltage sources.

## X matrix

The X matrix is an (n+m) by 1 vector that holds the unknown quantities (node voltages and the currents through the independent voltage sources). The top n elements are the n node voltages. The bottom m elements represent the currents through the m independent voltage sources in the circuit. The V matrix is n by 1 and holds the unknown voltages. The J matrix is m by 1 and holds the unknown currents through the voltage sources

## A matrix

The A matrix is (m+n) by (m+n) and will be developed as the combination of 4 smaller matrices, G, B, C, and D.

## generate the circuit equations

Use the str() function to convert sympy equations to strings. These strings can be copied to a new notebook.