Question: Please edit the below Python code and add the modifications under where it says, #EXTRA STUFF HERE!, to read the given files: PLEASE CLEARLY ADD

Please edit the below Python code and add the modifications under where it says, #EXTRA STUFF HERE!, to read the given files:

PLEASE CLEARLY ADD THE CODE MODIFICATIONS UNDER #EXTRA STUFF HERE! SECTIONS

################################################################################

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

# this is the list structure that we'll use to hold components:

# [ Type, Name, i, j, Value ]

################################################################################

# How large a matrix is needed for netlist? This could have been calculated #

# at the same time as the netlist was read in but we'll do it here #

# Input: #

# netlist: list of component lists #

# Outputs: #

# node_cnt: number of nodes in the netlist #

# volt_cnt: number of voltage sources in the netlist #

################################################################################

def get_dimensions(netlist): # pass in the netlist

### EXTRA STUFF HERE!

# print(' Nodes ', node_cnt, ' Voltage sources ', volt_cnt)

return node_cnt,volt_cnt

################################################################################

# 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,node_cnt):

# return the total number of rows in the matrix for

# error checking purposes

# add 1 for each voltage source...

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]

#EXTRA STUFF HERE!

return node_cnt # 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(" ")

#EXTRA STUFF HERE!

################################################################################

Read the below files, and do not edit them! Please edit the above Python code and add the modifications under where it says #EXTRA STUFF HERE!

READ_NETLIST.PY (READ FILE):

DO NOT MODIFY!!!!

################################################################################

# read netlist file and create a list of lists... #

################################################################################

import comp_constants as COMP # get the constants needed for lists

from sys import exit # needed to exit on error

################################################################################

# Read a netlist from a spice-like text file #

# Input: #

# none #

# Outputs: #

# netlist: a list of components, each component as a list #

# #

# this is the list structure that we'll use to hold components: #

# [ Type, Name, i, j, Value ] #

################################################################################

def read_netlist(): # read a netlist - no input argument!

filename=input("enter netlist text file name: ") # ask for the netlist

#print(filename) # debug statement

fh = open(filename,"r") # open the file

lines = fh.readlines() # read the file

fh.close() # close the file

netlist = [] # initialize our list

for line in lines: # for each component

line=line.strip() # strip CR/LF

if line: # skip empty lines

# reads: name, from, to, value

# so we need to insert the node type at the start of the list

# parse properties delimited by spaces

props = line.split(" ")

if ( props[COMP.TYPE][0] == COMP.RESIS ): # is it a resistor?

props.insert(COMP.TYPE,COMP.R) # insert type

props[COMP.I] = int(props[COMP.I]) # convert from string

props[COMP.J] = int(props[COMP.J]) # convert from string

props[COMP.VAL] = float(props[COMP.VAL]) # convert from string

netlist.append(props) # add to our netlist

elif ( props[COMP.TYPE][0:2] == COMP.V_SRC ): # a voltage source?

props.insert(COMP.TYPE,COMP.VS) # insert type

props[COMP.I] = int(props[COMP.I]) # convert from string

props[COMP.J] = int(props[COMP.J]) # convert from string

props[COMP.VAL] = float(props[COMP.VAL]) # convert from string

netlist.append(props) # add to our netlist

elif ( props[COMP.TYPE][0:2] == COMP.I_SRC ): # a current source?

props.insert(COMP.TYPE,COMP.IS) # insert type

props[COMP.I] = int(props[COMP.I]) # convert from string

props[COMP.J] = int(props[COMP.J]) # convert from string

props[COMP.VAL] = float(props[COMP.VAL]) # convert from string

netlist.append(props) # add to our netlist

else: # unknown component!

print("Unknown component type: ",line) # bad data!

exit() # bail!

return netlist

COMP_CONSTANTS.PY (READ FILE):

DO NOT MODIFY!!!!

#######################################################################

# Define some constants we'll use to reference things

RESIS = 'R' # a resistor

V_SRC = 'VS' # a voltage source

I_SRC = 'IS' # a current source

# Define the list data structure that we'll use to hold components:

# [ Type, Name, i, j, Value ] ; set up an index for each component's property

TYPE = 0 # voltage source or resister

NAME = 1 # name of the component

I = 2 # "from" node of the component

J = 3 # "to" node of the component

VAL = 4 # value of the component

# Define the different types of component

R = 0 # A resistor

VS = 1 # An independent voltage source

IS = 2 # An independent current source

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!