Week 6 - Data analysis
AIM: To script a program that reads through a file and performs the following operations
- Take column information as input and display, save the plot
- Inform the user if the data file isn’t present in the working directory
- Calculate the work done, power output and the specific fuel consumption
DATASET:
The given dataset has 17 columns containing thermodynamic data of a fuel injected engine. Text data that helps the user understand the data set begins with a # symbol. Hence all lines starting without a # has to be stored to different arrays.
SPECIAL FUNCTIONS:
simps() is used to calculate the area of PV diagram as per the third objective.
It is a part of scipy.integrate library and the following command has to be used in the initial code
from scipy.integrate import simps
This function works on the simpsons rule where it uses a quadratic polynomial on each subinterval of a partition to approximate the function f(x) and to compute the definite integral.
Simpsons rule is an improvement over the trapezoid rule.
Syntax: simps(dependent variable, independent variable)
If f(x) has to be integrated over x, then, simps(f(x), x is to be used).
FORMULAE REQUIRED:
Power output = Work done x No. of power strokes
For 2 stroke engine, No. of power strokes= rpm of engine
For 4 stroke engine, No. of power strokes= rpm of engine/2
Specific fuel consumption = Fuel consumption per unit time / Power
CODE:
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import simps
#Compatibilty check
try:
open('engine_data.out')
except:
print('File not recognized. Please provide a valid CONVERGE output file')
else:
line_ctr = 1 #Counter variable
#Arrays to store the parameter details
column_number = []
coloumn_name = []
units =[]
X = []
Y = []
#Taking user input
x = int(input("Enter x axis parameter"))
y = int(input("Enter y axis parameter"))
for line in open('engine_data.out'):
for i in range(0, 19):
if i >= 2:
if line_ctr == 2:
column_number.append(int(line.split()[i]))
for j in range(0, 18):
if j >= 1:
if line_ctr == 3:
coloumn_name.append(line.split()[j])
elif line_ctr == 4:
units.append(line.split()[j])
if '#' not in line:
if line_ctr >= 6:
X.append(float(line.split()[x]))
Y.append(float(line.split()[y]))
line_ctr += 1
#Plotting
plt.figure()
plt.plot(X, Y, color = 'r', linewidth = 2)
plt.title('Plot of ' + str(coloumn_name[y]) + ' versus ' + str(coloumn_name[x]), color = 'k')
plt.xlabel(str(coloumn_name[x]) + " " + str(units[x]))
plt.ylabel(str(coloumn_name[y]) + " " + str(units[y]))
plt.savefig(str(coloumn_name[y]) + ' vs ' + str(coloumn_name[x]) + '.png')
plt.show()
#------------------------------------------------------------#
#Constant parameters for engine
#Speed of engine (RPM)
N = 1500
#Fuel consumption (micro gram/stroke)
fc = 20
#Number of cylinders
n = 1
#Engine Specifications
no_of_fc = 4
#Calculation of area under PV diagram
#Workdone in joule
area = simps(Y, X)*pow(10, 6) #using simps function
print('Area of PV diagram(work done) calculated using simpsons rule, W = ' + str(area) + ' J','\n')
#Power Output (KW)
power = n*area*N/(60*1000)
print('Power Output at 1500 RPM, Power output= ' + str(power) + ' KW')
print('\n')
#Speed of engine (rph)
N_hr = N*60
#Fuel consumption (kg/rev)
fc_hr = fc*pow(10,-9)/2
#Fuel consumption (kilogram per hour)
fc_kg_hr = N_hr*fc_hr
#Specific fuel consumption
SFC = fc_kg_hr/power
print('Specific Fuel Consumption, SFC = ' + str(SFC) + ' (kg/hr)/KW')
print('\n')
FORMULAE REQUIRED:
Power output = Work done x No. of power strokes
For 2 stroke engine, No. of power strokes= rpm of engine
For 4 stroke engine, No. of power strokes= rpm of engine/2
Specific fuel consumption = Fuel consumption per unit time / Power
CODE:
EXPLANATION OF CODE:
- Import all libraries
- Use try: except: else: (Exception handling tool) to confirm that the file is present in the working directory. If not present, display an error
- Define variables to store the line number, column data, unit data, column thermodynamic information
- Request the user to enter the columns he/she wants to visualize
- Read through the file data and check if the data belongs to column number/name/units and split and write into the corresponding arrays.
- If the compiler does not encounter a # at the beginning, write the data into their respective arrays
- Plot the figure and save it with column names
For calculating the work done, power output and specific fuel consumption
- Define variables for all engine parameter inputs and assign value
- Calculate the area of PV diagram as explained in the earlier section
- Convert all the parameters to a single unit system (kW, h etc.)
- Calculate the specific fuel consumption as per the above described formula
- Display the values
RESULTS:
Output console
Input was x=7 and y=1
The PV diagram is plotted
The user can enter other values for input also, which will produce different plots
1. When x=0, y=1
2. When x=0, y=7
3. When x=0, y=4
4. When x=7, y=4
5. When x=7, y=1
ERRORS:
No errors were displayed in the console.