Project

Data analysis ||| Python program

OBJECTIVE:

Code to obtain the data from the converge output file & plot the graph according to the user input:

import numpy as np 
import matplotlib.pyplot as plt


try:
	open('engine_data.out')

except:
	print('file not in location / wrong input ')

Pressure = []
Volume = []
line_count=0
print('Enter the number for the columns to plot the graph:')
z1 =  int(input('Enter the first column: ')) 
z2 =  int(input('Enter the second column: '))
C1 = []
C2 = []
var = []
units = []
text_file = open('engine_data.out')
for line in text_file:
	if '#' in line:
		if line_count==2:
			var = line.split()
		if line_count==3:
			units = line.split()
		line_count = line_count + 1

	if '#' not in line:
		C1.append(float(line.split()[z1]))
		C2.append(float(line.split()[z2]))

if z1==1 and z2 == 7:

        A= np.trapz(C1,C2)
        p = A*1500/60
        print('Area under PV diagram: ',A)
        print('power output of the engine: ',p,'MW')


var = np.array(var[1:18])


plt.plot(C1,C2)
plt.xlabel(var[z1] + units[z1+1])
plt.ylabel(var[z2]+ units[z2+1])
plt.grid(color='r')
plt.savefig(str(var[z1]+' vs '+var[z2] + '.png' ))
plt.show()

Procedure:

  • 'math' and 'matplotlib.pyplot' were imported for using mathematical functions and plotting.'numpy' was imported for using 'trapz' command.
  • A 'for loop' was created to read all the lines(rows) of the datasheet and an 'if-condition' was given within the 'for loop' to read only the numerical data of the file from these rows. In order to store the values of a column to the specified array, indexing was used and the values were appended to the previously created empty arrays.
  • 'input' function was used to obtain the two column inputs (X and Y) from the user.
  • An empty array of Pressure, Volume, C1, C2, var & units has been created to append each of the required values and store the.
  • In order to show the correct labels on the graph, an array (C1) was formed. An 'if-condition' was specified for reading only the 3rd line of the data set and a 'for loop' was formed within it to append all the parameters of the row to the (C1) array. The same process was repeated for forming array C2 which contains the units of all the parameters.'xlabel' and 'ylabel' commands were used with inputs as the index value of the C1 and C2 arrays (which is given by the user) to show the appropriate labels on the graph.'savefig' command was used with inputs as index value of C1 array to save the plot generated with appropriate file names.
  • The area under the PV curve is obtained by using the 'trapz' command and giving the pressure and volume arrays as input. This area also represents the work done.
  • The power output was calculated using the formula 'p = A*1500/60'

Results:

  • The program asks the user for two input columns and then the program plots the graph between the columns provided as shown in the figure.
  • The script is written in such a way that if the user inputs for p-v plot, only then it would calculate the Area under PV diagram: 0.0005005866274680075 and power output of the engine: 0.012514665686700186 MW.
  • The p-v plot :


    Pressure vs Volume
  • The graph which is plotted is saved by plt.savefig(str(var[z1]+' vs '+var[z2] + '.png' ))
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.