Week 6 - Data analysis
Aim:
To be able to write the program to read the data, check the data, calculate the work done by the engine along with specific fuel consumption of the engine, and plot the graphs that are required for analysis.
Theory:
Data analysis is a process of inspecting, cleansing, transforming, and modeling data to discover useful information, inform conclusions, and support decision-making. Data analysis has multiple facets and approaches, encompassing diverse techniques under a variety of names, and is used in different business, science, and social science domains. In today's business world, data analysis plays a role in making decisions more scientific and helping businesses operate more effectively.
Python is an increasingly popular tool for data analysis. In recent years, several libraries have reached maturity, allowing Stata users to take advantage of the beauty, flexibility, and performance of Python without sacrificing the functionality these older programs have accumulated over the years.
Objectives:
TASK 1 - To make a Data visualizer:
The script should take column numbers as the input and plot the respective columns as separate images. Each file should be saved by the name of the column
The plot labels should be extracted from the file.
TASK 2 - Compatibility check:
code should exit gracefully if a non-compatible file is provided as input. It should say something like "File not recognized. Please provide a valid CONVERGE output file"
TASK3 - Basic performance calculation:
Calculate the area under the P-V diagram. P is in column 2 and V is in column 8.
Calculate the power output of this engine. Assume that RPM is 1500
This engine consumed 20 micrograms of fuel (per 1-stroke cycle). Calculate its specific fuel consumption.
PROCEDURE:
1] Import the modules such as Math, Matplotlib, Numpy, and scipy to perform their respective functions
2] To plot the curves between 2 quantities as per the user input, a line counter is initiated from 1. Now an "if" loop is iterated with the condition if '#' is not in line all the numerical values present in the data file are now to be stored in the data array as a string. Now another "if" loop is executed with the condition that the line counter is greater than or equal to 6 all the data will be stored in the pressure and volume array respectively.
3] For Engine performance calculations, the area under the PV diagram has to be calculated and we are using the Simpson method for calculating it.
4] Two arrays of names of parameters and their units are made as per the data file given and then the user has to choose from them to plot respective graphs. For this 2 inputs of the user are taken using the input command and the data type is set as 'int'. The user enters the column number as input and results are generated.
PROGRAM:
import matplotlib.pyplot as plt
import numpy as np
from numpy import trapz
filename = input("Input_File_Name : ")
if filename == 'engine_data.out':
print('valid file')
else:
print('invalid file')
exit()
def compatibility_check(a):
if 'CONVERGE' not in a:
print('please provide converge output file to process')
exit()
else:
print('compatibility check is successful')
pass
FILE = []
for line in open(filename):
if '#' in line:
FILE.append(line.split())
compatibility_check(FILE[0])
noc = len(FILE[1])
head = len(FILE[2])
col_no = []
col_val = []
col_units = []
z = FILE[1].index('column')
for i in range(z,noc-1):
col_no.append(i)
for j in FILE[2]:
if j is not'#':
col_val.append(j)
for k in FILE[3]:
if k is not'#':
col_units.append(k)
print('available data are:')
for a in range(0,len(col_no)):
print(col_no[a],col_val[a],col_units[a])
xaxis = int(input('Enter xaxis column no:'))-1
yaxis = int(input('Enter yaxis column no:'))-1
x = []
y = []
for line in open(filename):
if '#' not in line:
x.append(float(line.split()[xaxis]))
y.append(float(line.split()[yaxis]))
plt.plot(x,y,linewidth=1.5)
plt.xlabel(col_val[xaxis])
plt.ylabel(col_val[yaxis])
plt.show()
pressure = []
volume = []
for line in open(filename):
if '#' not in line:
pressure.append(float(line.split()[1]))
volume.append(float(line.split()[7]))
Pressure = 1e6*np.array(pressure)
Volume = np.array(volume)
area = np.trapz(Pressure,Volume)
print('workdone =',area, 'joules')
cyclepersec = 750/60
power = area*(cyclepersec/1000)
print('power=',power,'kw')
mass_flow = 20e-6
sfc = mass_flow*3600/power
print('specific fuel consumption =',sfc,'g/kwh')
Code Explanation:
1. In the first few lines, the required modules are imported. Then, we define a conditionally recursive function to check thefile format. If the file is not in the CONVERGE output format, we are prompting the user to enter the file name again.
2. If the file is not found, the code exits with an error message to the terminal.
If everything is correct, the user will be prompted to enter the two-column numbers to plot the graph. After the user enters the column numbers, we use for loop to get the names and the values of those columns and store them.
3. Then we calculate essential performance as asked in the question. The governing equations are used to calculate the required parameters. Then, we are plotting the graph between the user-entered columns, and the PV area curve.
FILE PARSING:
>Import the Plotting module:
We need to import the plotting module as shown in the above snap for plotting the different types of graphs for showing the variation concerning two parameters like volume, pressure temperature, etc.
>Import the NumPy module:
To give power to each element of an array or to perform any mathematical operations (like addition or subtraction) either on every element of a collection or between the arrays, we need to import the numpy library as shown above.
>Import the tarp from Numpy
To import the tarp from the numpy library, as shown above, to find out the area by trapezoidal numerical method.
> Checking the input file and compatibility:
As per data analysis, we need to import the correct file into the software and we need to read the entire file to analyze the data. So, we can input the correct file and check the compatibility through converge thus it will be passed as shown in abovepic.
>Appending the converge data:
In the beginning, we need to check the converge is starting with the # key. Splitting the line by line using the split function.Assigning the splitted lines to the loop with variables i, j, and k as shown in the above screenshot. Then finally append the data to the program. The program is about the method of indexing the specified value for analysis.
>>Program for asking the parameters for the x-axis and y-axis for plotting the graphs:
This is about creating the input variables that we need to plot for the x-axis and y-axis. This is user-friendly because the input variables will recognize the data and give the results that are required.
>>Calculating the required output:
The above program is a basic one in which the input is the formula to find out the work done, power, and specific fuel consumption.
Results:
We obtained the required graphs and the compatibility check was successful. From the output code, we can see that,
The area under the PV curve is 0.500050058 m
The engine power output is 0.0125 KiloWatts
Specific Fuel Consumption is 0.001598 g/KWhr
Conclusion:
The Python program ran successfully and ouputs are obtained.