EE20084: Structured Programming Coursework S. R. Pennock & J. Graham-Harper-Cater February 17, 2021 1 Introduction This programming coursework is designed to allow you to develop your structured program- ming skills in the context of an engineering problem using Python. The main objective of the coursework is to write a program that should analyse an electrical circuit, as described below. There are a number of extensions possible to the main objective that will allow you to gain additional marks. There is an all day laboratory session where the main aim is producing a Design Report on what is to be implemented, and one and a half days laboratory session where you should implement, debug and test the code. You are, of course, free to develop and test the code in your own time. Automated tests will be used on your submission to as- sess a mark and so it is critically important that you ensure that your code complies with the syntax and file formats described in this document. 2 The Objective Your objective is to write a program that analyses cascade circuits, such as shown in Fig- ure 2.1, where series and shunt resistances of any value can be connected in any order between a source and a load. This is exactly the same problem that underpins expensive circuit analysis software. For these circuits the ABCD matrix analysis scheme outlined in the Appendix is a natural, and simple, way to analyse the circuit. Figure 2.1: A cascade circuit of series and shunt resistances. Node numbers, 1 to 6 in this case, are used to define how components are connected. Node zero is the common (ground?) connection. 1 EE20084: Structured Programming Coursework Your program must perform the following tasks: 1. Read in the circuit input file using the format specified below. 2. Analyse the circuit to evaluate the requested output variables. 3. Write the output variables to a file in the specified format. You may extend the work in any way that you wish, so long as it is obvious and does not interfere with completion of the automated tests. For example your program may display a representation of the circuit under analysis. Your program should display a message that explains any such extensions you chose to make. 3 Running the program The program is to read an input data file, say ’test.net’, and output to a results file, say ’test.csv’. These files are to be specified in the command line, so to run ’MyCode.py’ on these files the command line should be python MyCode.py test.net test.csv 4 Input and Output file formats and syntax The files that describe the circuit (the input) and the results (the output) are described here. Your program should be as tolerant as possible of format errors in the input files and as precise as possible in the format of the output files. Note that because an automated tester will be used to assess your output files you must comply exactly with the file format described here. 4.1 Input file The input file contains a description of the circuit that is to be analysed. Lines that start with the hash symbol (#) are comments. The input file will contain three blocks, each of which is described in greater detail below: CIRCUIT block: This is delimited by
and . It defines the compo- nents in the circuit and how they are connected to each other. TERMS block: This is delimited by
and . It defines the source and the load. OUTPUT block: This is delimited by
. It defines the output filename to use, the variables to be printed out into the file and the units to be used for each variable. The following example is available on the Moodle site as ‘a_Test_Circuit_1.net’. S. R. Pennock & J. Graham-Harper-Cater Page 2 February 17, 2021 EE20084: Structured Programming Coursework 4.1.1 CIRCUIT block The start of the CIRCUIT block is indicated by the xml style text
and the end by . Each resistor component in the circuit is defined by two node numbers, one for each end of the component, and either its resistance, R, or its conductance G=1/R. # define a circuit between
and delimiters # Elements have two node numbers and either a # resistance R=1/G Ohms or a conductance G=1/R Seimens.
n1=1 n2=2 R=47 n1=2 n2=0 R=100 n1=2 n2=3 R=150 n1=3 n2=4 R=330 n1=4 n2=0 G=0.0125 n1=4 n2=0 R=82 n1=4 n2=5 R=25 n1=5 n2=0 R=280 # components do not have to follow their order in the circuit n1=6 n2=0 R=100 n1=5 n2=6 G=0.02 Note that components do not have to be specified in the order that they appear in the circuit, the node numbers show how the components are connected. S. R. Pennock & J. Graham-Harper-Cater Page 3 February 17, 2021 EE20084: Structured Programming Coursework 4.1.2 TERMS block The TERMS block defines the source and the load attached to the circuit. The source is implicitly connected between node 0, the common node, and node 1. The source may be given as a Thevenin source or a Norton source. The load is connected between the implicit node 0 and the last node specified in the CIR- CUIT block. Figure 4.1: Thevenin and Norton sources. Equivalent when VS = IS RS. # define the terminations between
and delimiters
# a 5V Thevenin voltage source with RS=50 ohms connected # between node 1 and the implicit common (0) node VT=5 RS=50 # or a 2.5 Amp Norton current source with RS=25 Ohms #IN=2.5 RS=25 #IN=2.5 GS=0.04 # load connected between last node (6 in this case) and the implicit common (0) RL=75 Fstart=10.0 Fend=10e+6 Nfreqs=10 # Frequency start, end and number of frequencies S. R. Pennock & J. Graham-Harper-Cater Page 4 February 17, 2021 EE20084: Structured Programming Coursework 4.1.3 OUTPUT block The OUTPUT block defines the desired output of the circuit analysis, including a filename where the output variables will be written. It defines which variables are to be written, in what order, and the units to be written for each variable. The variables to be output can be: • Vin - Input Voltage • Iin - Input Current • Pin - Input Power • Zin - Input Impedance • Vout - Output Voltage • Iout - Output Current • Pout - Output Power • Zout - Output Impedance • Av - Voltage Gain • Ai - Current Gain Note: input values are defined as being measured between nodes 0 and 1 (through node 1 for current). Output values are defined as being measured between the node 0 and the load node, which is the last node that is defined in the CIRCUIT block of the input file. # define the outputs between
delimiters # each line has a parameter and its units if there are any.
S. R. Pennock & J. Graham-Harper-Cater Page 5 February 17, 2021 EE20084: Structured Programming Coursework 4.1.4 Output file format The first line of the output file should be the variable type. The second line of the output file should be the units for the output variables and the third and subsequent lines are the variable values. Apart from the frequency the output values are all complex numbers in either real and imaginary format, or when using decibel outputs in magnitude and phase. The commas following the labels and the data should all align. The units for a linear gains should be L. The following output results from the example input file ‘a_Test_Circuit_1.net’. The circuit only contains just resistors, so the output is the same at all frequencies: Freq, Re(Vin), Im(Vin), Re(Vout), Im(Vout), Re(Iin), Im(Iin), Re(Iout), Im(Iout), Re(Pin), Im(Pin), Re(Zout), Im(Zout), Re(Pout), Im(Pout), Re(Zin), Im(Zin), Re(Av), Im(Av), Re(Ai), Im(Ai) Hz, V, V, V, V, A, A, A, A, W, W, Ohms, Ohms, W, W, Ohms, Ohms, L, L, L, L 1.000e+01, 3.615e+00, 0.000e+00, 4.387e-02, 0.000e+00, 2.769e-02, 0.000e+00, 5.849e-04, 0.000e+00, 1.001e-01, 0.000e+00, 5.029e+01, 0.000e+00, 2.566e-05, 0.000e+00, 1.306e+02, 0.000e+00, 1.213e-02, 0.000e+00, 2.112e-02, 0.000e+00 1.111e+06, 3.615e+00, 0.000e+00, 4.387e-02, 0.000e+00, 2.769e-02, 0.000e+00, 5.849e-04, 0.000e+00, 1.001e-01, 0.000e+00, 5.029e+01, 0.000e+00, 2.566e-05, 0.000e+00, 1.306e+02, 0.000e+00, 1.213e-02, 0.000e+00, 2.112e-02, 0.000e+00 2.222e+06, 3.615e+00, 0.000e+00, 4.387e-02, 0.000e+00, 2.769e-02, 0.000e+00, 5.849e-04, 0.000e+00, 1.001e-01, 0.000e+00, 5.029e+01, 0.000e+00, 2.566e-05, 0.000e+00, 1.306e+02, 0.000e+00, 1.213e-02, 0.000e+00, 2.112e-02, 0.000e+00 . . . S. R. Pennock & J. Graham-Harper-Cater Page 6 February 17, 2021 EE20084: Structured Programming Coursework 5 Additional extension features The following additional extension features can be added in order to gain more marks. 5.1 Exponent prefixes Add the exponent prefixes (p, n, u, m, k, M, G) and use them in reading and writing all input and output variables. Symbol Prefix Factor p pico 10−12 n nano 10−9 u micro (µ) 10−6 m milli 10−3 k kilo 103 M mega 106 G giga 109 Decibels and phase Add the ability to express the outputs in decibels. The