Project

DATA ANALYSIS USING PYTHON

AIM of the Project:

To write a Python Code to read data from a file containing engine's data and create a data visualizer

tool. The program must also find out the power output and the specific fuel consumption of the engine

by plotting the PV diagram of the engine cycle.

Code of the Project:

"""
File Parsing using Python

"""

import matplotlib.pyplot as plt
import math
import numpy as np
from numpy import trapz

crank = []
pressure = []
temperature = []
volume = []
DataArray = []
Parameters =[]

try:
	open('engine_data.out')

except:

	print("File Not Recognized. Please provide a valid CONVERGE Output File.") 

ct = 1

with open('engine_data.out') as f:
	for line in f:
		if '#' not in line:
			DataArray.append([float(i) for i in line.split()])


with open('engine_data.out') as l:

	for line in l:

		if '#' in line:

			for i in range(3,5):
				if lct is i:
					#print(line.split())
					Parameters.append([str(k) for k in line.split()])
			lct = lct + 1

for s in range(0,2):
	del(Parameters[s][0])

m = input("Enter the column number:")
n = int(m)

plotarray = []

for z in range(0,len(DataArray)):
	plotarray.append((DataArray[z][n-1]))

b = Parameters[0][n-1]
print(b)

plt.figure(1)
plt.plot(plotarray)
plt.ylabel(b)
plt.savefig('%s.png' %b)

a = input("Enter the Column number for Pressure:")
e = int(a)
b = input("Enter the Column number for Volume:")
f = int(b)

for r in range(0,len(DataArray)):
	pressure.append(DataArray[r][e-1])
	volume.append(DataArray[r][f-1])

xvalues = []
yvalues = []
xvalues = volume
yvalues = pressure

Area = np.trapz(yvalues,xvalues)
print(Area)

plt.figure(2)
plt.plot(volume,pressure)
plt.xlabel('Volume')
plt.ylabel('Pressure')
plt.savefig('PV Diagram' + '.png')
plt.show()

massflowrate = 20 #grams/stroke cycle
ns = 4 #Number of Strokes - 2 revolutions of the crank shaft per 4 strokes (1 Cycle)
Speed = 1500 # RPM...1500 rotations in 60 secs
# Since 2 rotations make 1 Stroke Cycle...Therefore...engine makes 750 Strokes in 60 secs, i.e, 1 stroke cycle occurs in 0.08 secs.
# Therefore, mass flow rate in kg/s is 20 * 10^-6/0.08 i.e, 250 * 10 ^-6 kg/sec.
mfr = 250*1e-06
Work = Area * 1e+06
Power = Work * (Speed/120)
Power = Power/1000
SFC = mfr / Power
print(' Power Output of the Engine is: ' , Power , 'kW.')
print(' Specific Fuel Consumption is: ' , SFC, 'g/kWh.')

Explanation of the Code:

"""

File Parsing using Python

"""

import matplotlib.pyplot as plt

# Importing matlplotlib.pyplot as plt to make python work as Matlab. matplotlib.pyplot is a

collectionof functions used for various activities such generating figures, generating plots,

etc.

import math

import numpy as np

# Importing 'numpy' as 'np'. numpy is a core library to perform all the scientific calculations

in Python.

from numpy import trapz

# Importing the function 'trapz' from the library 'numpy' to find out the area of plot using the

trapezoidal numerical method.

crank = []

# Defining an empty array 'crank'.

pressure = []

# Defining an empty array 'pressure' to store tha values of pressure.

temperature = []

# Defining an empty array 'temperature' to store tha values of pressure.

volume = []

# Defining an empty array 'volume' to store tha values of pressure.

DataArray = []

# Definfing an empty array 'DataArray' to store all the data in the file.

Parameters =[]

# Defining an empty array 'Parameters' to store the names of the properties in the file, along with

their units.

try: open('engine_data.out')

# Using the 'try' and 'open' command to open the file and check the compatibilty of the file.

except: print("File Not Recognized. Please provide a valid CONVERGE Output File.")

# Using the except command to display the error, if the file doesn't get opened.

ct = 1

# Defining the counter variable 'ct' and initializing it by 1.

with open('engine_data.out') as f:

# Using 'with' command to open file as a variable 'f' containing the data of the file line by line.

for line in f:

# Starting a for loop to read the contents of 'f' line by line, using the variable 'line'.

if '#' not in line:

# Declaring an if statement with the condition to check if the lie being read contains the symbol '#' or

not.

DataArray.append([float(i) for i in line.split()])

# Splitting the current line being read by usling the 'split()' function.

# Accessing the splitted line using a for loop with the varibale 'i'.

# Converting all the splitted content of the line into floating numbers using the 'float()' function.

# Storring all the converted data in the array 'DataArray' using the 'append' command.

with open('engine_data.out') as l:

# Using 'with' command to open file as a variable 'f' containing the data of the file line by line.

for line in l: if '#' in line:

# Starting a for loop to access the content of 'l' line by line and at the same time providing an if

statement with the condition that the line being read contains the symbol '#' or not.

for i in range(3,5):

# Starting a for loop using the variable 'i' with its values in the range from 3 to 4. 5th value is not

included as per the governing functions of Python. In python the indexing starts from '0'.

if lct is i:

# Providing an 'if' statement to check if the value of the variable 'lct' matches with the current value of

the variable 'i'.

Parameters.append([str(k) for k in line.split()])

# Splitting the current line being read by usling the 'split()' function.

# Accessing the splitted line using a for loop with the varibale 'k'.

# Converting all the splitted content of the line into floating numbers using the 'str()' function.

# Storring all the converted data in the array 'Parameters' using the 'append' command.

lct = lct + 1

# Incrementing the value of 'lct'.

for s in range(0,2): del(Parameters[s][0])

# Deleting the first two lines from the 'Parameters' array.

m = input("Enter the column number:")

# Using the 'input()' function to ask for user input about the number of column.

n = int(m)

# Converting the value of the user input into an integer, since in Python every input data is stored as

a string.

plotarray = []

# Defining an empty array 'plotarray' to store the data of the corresponding column as entered by

the user.

for z inrange(0,len(DataArray)):

# Starting a for loop using the variable 'z' with its values in the range from 0 to the length of the

array 'DataArray'.

plotarray.append((DataArray[z][n-1]))

# Extracting the values of the data from the corresponding column of the array 'DataArray' as per the

input of the user.

b = Parameters[0][n-1]

# Defining a variable 'b' and assigning to it, the value from the array 'Parameters', that contains the

name of the property of the data matching the user's input.

print(b)

# Printing the value of 'b'.

plt.figure(1)

# Defining the first figure.

plt.plot(plotarray)

# Plotting the values of the data in the array 'plotarray'.

plt.ylabel(b)

# Adding the ylabel to the plot with name of the property stored in the variable 'b'.

plt.savefig('%s.png' %b)

# Using the function 'savefig' to save the current figure with name stored in the variable 'b'.

a = input("Enter the Column number for Pressure:")

# Defining a variable 'a' and storing in it the value of the user's input for the column number of

the property 'pressure'.

e = int(a)

# Converting the user's input into an integer and storing it in another variable 'e'.

b = input("Enter the Column number for Volume:")

# Defining a variable 'b' and storing in it the value of the user's input for the column number of the

property 'volume'.

f = int(b)

# Converting the user's input into an integer and storing it in another variable 'f'.

for r in range(0,len(DataArray)):

# Starting a for loop using the variable 'r' with its values in the range from 0 to the length of the array

'DataArray'.

pressure.append(DataArray[r][e-1])

# Storing all the values from the pressure column in the file to the array 'pressure'.

volume.append(DataArray[r][f-1])

# Storing all the values from the volume column in the file to the array 'volume'.

xvalues = []

# Defining an empty array 'xvalues' for plotting.

yvalues = []

# Defining an empty array 'yvalues' for plotting.

xvalues = volume

# Storing the values of the 'volume' array to 'xvalues' array.

yvalues = pressure

# Storing the values of the 'pressure' array to 'yvalues' array.

Area = np.trapz(yvalues,xvalues)

# Using the 'trapz' function from the 'numpy' library to find the area of the plot between pressure and

volume.

print(Area)

# Printing the value of the variable 'Area'.

plt.figure(2)

# Defining the second figure.

plt.plot(volume,pressure)

# Plotting the graph between pressure and volume.

plt.xlabel('Volume')

# Adding xlabel to the plot.

plt.ylabel('Pressure')

# Adding ylabel to the plot.

plt.savefig('PV Diagram' + '.png')

# Saving the figure using the 'savefig' function.

plt.show()

# Showing the plots.

massflowrate = 20

#grams/stroke cycle

ns = 4

#Number of Strokes - 2 revolutions of the crank shaft per 4 strokes (1 Cycle)

Speed = 1500

# RPM...1500 rotations in 60 secs

# Since 2 rotations make 1 Stroke Cycle...Therefore...engine makes 750 Strokes in 60 secs,

i.e, 1 stroke cycle occurs in 0.08 secs.# Therefore, mass flow rate in kg/s is 20 *10^-6/0.08

i.e, 250 * 10 ^-6 kg/sec.

mfr = 250*1e-06

# Defining a new variable and storing the value of the mass flow rate in kg/sec.

Work = Area * 1e+06

# Converting the work from MJ to J.

Power = Work * (Speed/120)

# Calculating the power output of the engine.

Power = Power/1000

# Converting power to kW.

SFC = mfr / Power

# Calculating the Specific Fuel Consumption of the engine.

print(' Power Output of the Engine is: ' , Power , 'kW.')

# Printing the value of the Power Output of the engine.

print(' Specific Fuel Consumption is: ' , SFC, 'g/kWh.')

# Printing the value of the Specific Fuel Consumption of the engine.

Outputs:

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.