hw3_ai

py

School

Arizona State University *

*We aren’t endorsed by this school

Course

591PYTHON

Subject

Electrical Engineering

Date

Jan 9, 2024

Type

py

Pages

2

Uploaded by UltraBoulderHare16

Report
import numpy as np # needed for arrays from numpy.linalg import solve # needed for matrices from read_netlist import read_netlist # supplied function to read the netlist import comp_constants as COMP # needed for the common constants # list format that we'll use to hold components is given below: # [ Type, Name, i, j, Value ] ################################################################################ # Input: # # netlist: list of component lists # # # # Outputs: # # node_cnt: count of nodes in netlist list # # volt_cnt: count of voltage sources in the netlist list # ################################################################################ def get_dimensions(netlist): voltage_count = 0 list1 = [] list2 = [] for item in netlist: list1.append(item[2]) list2.append(item[3]) if (item[0] == 1): voltage_count += 1 list1_max = max(list2) list2_max = max(list1) node_count = max(list1_max,list2_max) print(' Nodes ', node_count, ' Voltage sources ', voltage_count) return node_count,voltage_count ################################################################################ # Function to stamp the components into the netlist # # Input: # # y_add: the admittance matrix # # netlist: list of component lists # # currents: the matrix of currents # # node_cnt: the number of nodes in the netlist # # Outputs: # # node_cnt: the number of rows in the admittance matrix # ################################################################################ def stamper(y_add,netlist,currents,num_nodes): # return the total number of rows in the matrix for # error checking purposes # add 1 for each voltage source... (node_cnt,volt_cnt) = get_dimensions(netlist) current_row = node_cnt - 1 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.R ): # a resistor
if (i >= 0): # add on the diagonal y_add[i,i] += 1.0/comp[COMP.VAL] if (j >= 0): y_add[j,j] += 1.0/comp[COMP.VAL] if (i >=0 and j >=0): y_add[i,j] -= 1.0/comp[COMP.VAL] y_add[j,i] -= 1.0/comp[COMP.VAL] if (comp[COMP.TYPE] == COMP.VS): current_row += 1 currents[current_row] = comp[COMP.VAL] if (i >= 0): y_add[current_row,i] = 1 y_add[i,current_row] = 1 if (j >= 0): y_add[current_row,j] = -1 y_add[j,current_row] = -1 if (comp[COMP.TYPE] == COMP.IS): if (i >= 0): currents[i,0] = -comp[COMP.VAL] if (j >= 0): currents[j,0] = comp[COMP.VAL] voltage = solve(y_add,currents) #EXTRA STUFF HERE! return (voltage) # should be same as number of rows! ################################################################################ # Start the main program now... # ################################################################################ # 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") (node_cnt,volt_cnt) = get_dimensions(netlist) admittance = np.zeros((node_cnt+volt_cnt, node_cnt+volt_cnt)) currents = np.zeros((node_cnt+volt_cnt,1)) voltage = stamper(admittance, netlist, currents, node_cnt) print(voltage)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help