Python Program to Implement Stamp Matrix Components Assignment Solution.

Instructions

Objective
Write a program to implement stamp matrix components in python.

Requirements and Specifications

Homework 3 Description
In this homework problem we will learn to stamp components from a circuit into a matrix describing that circuit using the methods from nodal analysis in Circuits 1. Then we will apply this to a simple problem.
When running with a voltage source, the only output from your program must be the resulting voltage vector. Remember that the last element(s) in the vector is actually the current through the voltage source(s) when one, or more, voltage sources are included.
When running with the current source, the only output from your program must be the voltage vector.
You should submit hw3.py. Do not submit any of the below files (other than your significantly enhanced hw3.py).
NOTE: This means you MUST NOT change the constants you use!
You may use the schematics in the lecture as tests to see if your solution is correct. However, the grader will also run your code on other circuits which may have multiple current sources, voltage sources, or both, and far more resistors. So, make up some circuits of your own for which you can easily compute the answers to check your work.
You may assume that the nodes will be numbers 0-N consecutively so you need not worry about missing node numbers.
NOTE: Do not try to resize the arrays! You should be able to size the arrays correctly before populating them!
Source Code
```from netlist_stub import * from read_netlist import * from comp_constants import * ################################################################################ # Function to solve the circuit # # Input: # # y_add: the admittance matrix # # netlist: list of components # # Outputs: # # X: Vector of voltages # ################################################################################ def solve(y_add, currents, netlist): # We create the matrix A of size (m+n)x(m+n) (n: number of nodes, m: number of voltage sources # We get size m and n n, m = get_dimensions(netlist) A = np.zeros((m + n, m + n)) # The first [0:n, 0:n] elements in A are the elements of the admittance matrix A[0:n, 0:n] = y_add # We create matrix B of size nxm determined by the connection of voltage sources B = np.zeros((n, m)) # We do not create matrix C because C is the transpose of B and we will calculate it later # We create matrix D of sizes mxm D = np.zeros((m, m)) # Now, we create the matrix z that will store the current and voltage sources # This matrix will be formed by submatrices i_s and e, and will have a shape of (m+n)x1 z = np.zeros((m + n, 1)) # Matrix (vector) e to store voltage sources e = np.zeros((m, 1)) n_volt = 0 for comp in netlist: # for each component... # print(' comp ', comp) # which one are we handling... # extract the i,j and fill in the matrix... # subtract 1 since node 0 is GND and it isn't included in the matrix i = comp[COMP.I] - 1 j = comp[COMP.J] - 1 if (comp[COMP.TYPE] == COMP.VS): # voltage source if i >= 0: B[i, n_volt] = -1 if j >= 0: B[j, n_volt] = 1 e[n_volt] = -comp[COMP.VAL] # we add them as negative because it corresponds to the negative terminal n_volt += 1 # Calculate C C = B.T # Now, fill matrix A A[0:n, 0:n] = y_add A[0:n, n:(n + m)] = B A[n:(n + m), 0:n] = C # Construct z z[:n] = currents z[n:] = e # Now, compute VI_sol = np.matmul(np.linalg.inv(A), z) # Extract voltages X = VI_sol[:n] return X ################################################################################ # Start the main program now... # ################################################################################ if __name__ == '__main__': # Read the netlist! netlist = read_netlist() # Print the netlist so we can verify we've read it correctly """ for index in range(len(netlist)): print(netlist[index]) print("\n") """ #EXTRA STUFF HERE! # Get dimensions node_cnt, volt_cnt = get_dimensions(netlist) # Create admittance matrix y_add = np.zeros((node_cnt,node_cnt)) # Create vector that will store currents currents = np.zeros((node_cnt,1)) # Fill matrices node_cnt = stamper(y_add, netlist, currents, node_cnt) X = solve(y_add, currents, netlist) print(X)```