Week 6 - Data analysis
Aim: Data analysis using Python
Objectives:
- To write python code that should take column numbers as the input and plot the respective columns as separate images
- To save image file by the name of the column
- The plot labels should be extracted from the file.
- Calculate the area under the P-V diagram
- Calculate the power output of this engine. Assume that RPM is 1500
- This engine consumed 20 micro grams of fuel (per 1 stroke cycle). Calculate its specific fuel consumption.
Theory:
Data collected from thermodynamic analysis of IC engine is provided in the form of .out file. This data is extracted by using python program and necessary plots are plotted. This data contains 17 columns and 8000 values.
Following formulas are use to find out area under p-V diagram, power output of engine and specific fuel consumption.

Code explaination:
- Import the necessary modules.
- Take filename as an input and assign to'filename' variable.
- Perform compatibility test
- Checking if the fileis a valid CONVERGE output file
- Checking if data is present in the CONVERGE output file
- Data extraction (Extracting data along with the column name and column number from the CONVERGE output file. Complete data from the file stored in two D array 'data'. The columns of the data matrix contains the data for each individual parameters).
- Getting the data
- Provide an instruction to the user so that he/she can use the numbers as an input to designate the variablesto be plotted.
- Ask users of plot betweeen any two variablesis required. If input is y then ask for the numbers representing the variables to be plotted.
- Ask the user if the plot is to be saved or not
- Then show the plot
Above steps run in a while loop until user gives an input 'n' to the question " Do you want a plot between any two variables? (y/n)
- Calculation
- Extract volume and pressure data from matrix'data' and store in the 'volume' and'pressure' respectively. Then, Numerically integrate pressure and volume data using simps () function from scipy.integrate library. It is the work done by the engine.
- Take the value of engine speed (in rpm) and fuel consumption (in microgram) form user. Calculate the Power produced and Specific fuel consumption usinq the derived formulas.
Python code:
# Data analysis using python
import math
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import simps
filename = input("Enter the name of CONVERGE output filename with extension (.out):")
print(filename)
#COMPATIBILITY CHECK
#Checking if the file is a valid CONVERGE output file
try:
f = open(filename)
f.close()
except FileNotFoundError:
print('File not recognized. Please provide a valid CONVERGE output file')
exit()
#Checking if data is present in the CONVERGE output file
i=0
for line in open(filename):
if '#' not in line:
break
i = i+1
j=0
for line in open(filename):
j=j+1
if j>i:
break
if j==i:
print ('No data present in the CONVERGE output file')
exit()
#DATA EXTRACTION
i=0
for line in open(filename):
if i==2:
c = line.split() #Storing unit of variables in list c
i=0
break
if i==1:
b=line.split() #Storing names of variables in list b
i = i+1
if 'column' in line.split():
a = line.split()#Storing column numbers of variables in list a
i=1
a.remove("column") #Removing column from list a
a.remove("#") #Removing # from list a
a = np.array(a,dtype=np. int) #Converting data to integer
b.remove("#") #Removing # from list b
c.remove("#") #Removing # from list c
num_col = len(a) #Number of columns/number of variables
#GETTING THE DATA
data_counter = 0 #Initializing number of data points counter variable
data = np.arange(0,num_col)
for line in open(filename):
if '#' not in line:
d = line.split() #Storing current data line as in d
d = np.array(d,dtype=np.float32) #Converting list data to float array
data = np.vstack([data,d]) #Stacking the float array d in data matrix
data_counter = data_counter + 1
data = np.delete(data,0,0) #Deleting the first row used to initialize the array
#DATA VISUALIZER
print ('Type - ')
for i in a:
print ('{}for {}'.format(i,b [i-1]))
print(' ')
x = input("Do you want a plot between any two variables? (y/n):")
while(x.lower()=='y'):
cn1=input("Enter the clumn number of the variable along X-axis (Refer input map above):")
cn2=input("Enter the clumn number of the variable along Y-axis (Refer input map above):")
cn1=int(cn1)
cn2=int(cn2)
plt.figure()
plt.plot(data[:,cn1-1],data[:,cn2-1])
plt.xlabel('{} {}'.format(b[cn1-1],c[cn1-1]))
plt.ylabel('{} {}'.format(b[cn2-1],c[cn2-1]))
plt.title('{} {} vs {} {}'.format(b[cn2-1],c[cn2-1],b[cn1-1],c[cn1-1]))
z = "n"
z = input("Do you want to save the plot? (y/n):")
if(z.lower()=='y'):
plt.savefig("{}-{}.png".format(b[cn2-1],b[cn1-1]))
plt.show()
print (' ')
x = input("Do you want to plot any other two variables? (y/n):")
print (' ')
x = input("Do you want a single variable plot? (y/n):")
while(x.lower()=='y'):
cn = input("Enter the column number of the variable (Refer input map above):")
cn = int(cn)
plt.figure()
plt.plot(np.arange(0,data_counter),data[:,cn -1])
plt.xlabel('Data point number')
plt.ylabel('{}{}'.format(b[cn -1],c[cn -1]))
z="n"
z= input("Do you want to save the plot? (y/n):")
if(z.lower()=='y'):
plt.savefig ("{}.png" .format(b[cn-1]))
plt.show()
print ('')
x = input("Do you want any other single variable plot? (y/n): ")
print (' ')
#CALCULATIONS
print ("PERFORMANCE OF THE ENGINE")
#Wor k done
b1 = [i.lower() for i in b]
pressure_column = b1.index("pressure")
volume_column = b1.index("volume")
pressure = data [:,pressure_column]
volume = data [:,volume_column ]
A=0 #Area under the P-V curve
A = simps(pressure, volume,even= 'first ') *np.power (10,6) #Work per cycle(J)
#Power output
rpm = float(input('Enter the rpm of the engine at which data was collected:')) #Rotation per min
f = float (input('Enter the va ue of the fuel consumed per cycle (in micrograms):')) #Fuel consumption per cycle
n = rpm/(2*60) #Number of cycle per second for 4-st roke engine
P = A*n #Power output (W) at speed N (rad/s)
#Specific fuel consumption
f = (f*n*3600)/np.power(10,9) #Fuel consumption (Kg/hr)
sfc = ( f *1000)/P #Specifie fuel consumption (Kg/hr.KW)
#Results
print ('Area under the P-V curve ( i.e.the Work done per cycle) = {} J'.format(A))
print ('Power output at {} rpm = {} W'.format (rpm ,P))
print ('Specific fuel consumption = {} Kg/KW.hr'.format (sfc))
Results:
Enter the name of CONVERGE output filename with extension (.out):engine_data.out
engine_data.out
E:\Python\data analysis\data_analysis.py:59: DeprecationWarning: `np.int` is a deprecated alias for the builtin `int`. To silence this warning, use `int` by itself. Doing this will not modify any behavior and is safe. When replacing `np.int`, you may wish to use e.g. `np.int64` or `np.int32` to specify the precision. If you wish to review your current use, check the release note link for additional information.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
a = np.array(a,dtype=np. int) #Converting data to integer
Type -
1for Crank
2for Pressure
3for Max_Pres
4for Min_Pres
5for Mean_Temp
6for Max_Temp
7for Min_Temp
8for Volume
9for Mass
10for Density
11for Integrated_HR
12for HR_Rate
13for C_p
14for C_v
15for Gamma
16for Kin_Visc
17for Dyn_Visc
Do you want a plot between any two variables? (y/n):y
Enter the clumn number of the variable along X-axis (Refer input map above):8
Enter the clumn number of the variable along Y-axis (Refer input map above):2
Do you want to save the plot? (y/n):y
Do you want to plot any other two variables? (y/n):y
Enter the clumn number of the variable along X-axis (Refer input map above):1
Enter the clumn number of the variable along Y-axis (Refer input map above):2
Do you want to save the plot? (y/n):y
Do you want to plot any other two variables? (y/n):n
Do you want a single variable plot? (y/n):y
Enter the column number of the variable (Refer input map above):2
Do you want to save the plot? (y/n):y
Do you want any other single variable plot? (y/n): n
PERFORMANCE OF THE ENGINE
Enter the rpm of the engine at which data was collected:1500
Enter the va ue of the fuel consumed per cycle (in micrograms):20
Area under the P-V curve ( i.e.the Work done per cycle) = 500.58645159307406 J
Power output at 1500.0 rpm = 6257.330644913426 W
Specific fuel consumption = 0.00014383129981018482 Kg/KW.hr
***Repl Closed***
Saved plots


