Data analysis
Aim:- To write a code in Python that parses the CONVERGE file containing the data of an IC engine.
Theory:-
Otto cycle is the ideal cycle for spark-ignition reciprocating engines. It is named after Nikolaus A. Otto, who built a successful four-stroke engine in 1876 in Germany. In most spark-ignition engines, the piston executes four complete strokes (two mechanical cycles) within the cylinder, and the crankshaft completes two revolutions for each thermodynamic cycle.These engines are called four-stroke internal combustion engines.
Net work done and specific fuel consumption are the two important parameters that decide the performance of these engines. For a closed system, the area under pressure versus volume graph gives the net work done. Thus we parse the text file of an IC engine obtained from the simulation in CONVERGE and calculate the properties.
Code:-
#Importing libraries
import matplotlib.pyplot as plt
import numpy as np
#defining empty arrays
crank=[]
pressure=[]
max_pressure=[]
min_pressure=[]
temp=[]
max_temp=[]
min_temp=[]
volume=[]
mass=[]
density=[]
Int_HR=[]
HR_rate=[]
C_p=[]
C_v=[]
gamma=[]
Kin_visc=[]
Dyn_visc=[]
#combining all arrays into single array
prop=[crank,pressure,max_pressure,min_pressure,temp,max_temp,min_temp,volume,mass,density,Int_HR,HR_rate,C_p,C_v,gamma,Kin_visc,Dyn_visc]
#asking input
s=input("Enter the file Pathn")
#function for plotting based on users input
def user_input(x_prop,y_prop):
line_count=1
for line in open(s,'r'):
if line_count == 3:
l=(line.split()[0:])
line_count=line_count+1
plt.plot(prop[x_prop-1],prop[y_prop-1])
plt.xlabel(l[x_prop])
plt.ylabel(l[y_prop])
name=l[y_prop]+' vs '+l[x_prop]
plt.title(name)
#using try & expect to check and read file
try:
f1=open(s,'r')
a=f1.readline()
name=a.split()[1]
if name=='CONVERGE':
for line in f1:
if '#' not in line:
#reading lines without # and storing values to respective arrays
crank.append(float(line.split()[0]))
pressure.append(float(line.split()[1]))
max_pressure.append(float(line.split()[2]))
min_pressure.append(float(line.split()[3]))
temp.append(float(line.split()[4]))
max_temp.append(float(line.split()[5]))
min_temp.append(float(line.split()[6]))
volume.append(float(line.split()[7]))
mass.append(float(line.split()[8]))
density.append(float(line.split()[9]))
Int_HR.append(float(line.split()[10]))
HR_rate.append(float(line.split()[11]))
C_p.append(float(line.split()[12]))
C_p.append(float(line.split()[13]))
gamma.append(float(line.split()[14]))
Kin_visc.append(float(line.split()[15]))
Dyn_visc.append(float(line.split()[16]))
#printing the properties and respective no.s
print("Propertiest tColumn Number")
print("Crankttt 01nPressurett 02nMaximum Pressuret 03nMinimum Pressuret 04nTemperaturett 05nMaximum Temperaturet 06nMinimum Temperaturet 07nVolumettt 08nMassttt 09nDensityttt 10nIntegrated HRtt 11nHR Ratettt 12nCpttt 13nCvttt 14nGammattt 15nKinematic Viscosityt 16nDynamic Viscosityt 17n")
print("To get plot, Please enter column number of respective propertyn")
#asking inputs for axis and calculating the work, power and specific fuel consumption
invalid_input = True
def start():
x_prop=int(input("X-axis:"))
y_prop=int(input("Y-axis:"))
if not(0 < x_prop < 18):
print("Enter number between 1 to 17")
invalid_input = False
else:
if not(0 < y_prop < 18):
print("Enter number between 1 to 17")
invalid_input = False
else:
user_input(x_prop,y_prop)
work=np.trapz(pressure,volume)*1000 #to get the work done we take the area under pv diagram
print("nWork Done = {:f} KJ".format(work))
rpm=1500/2 #assuming foure stroke, power is produced in alternate cycle
rps=rpm/60
power=work*rps
print("Power = {:f} KW".format(power))
fuel=20*pow(10,-6)*rps*3600 #fuel consumed per stroke is 20 micro grams, fuel consumed per hour equals number of intake strokes(every alternate cycle) in an hour
sfc=fuel/power
print("SFC = {:f} g/KWh".format(sfc))
plt.show()
#if inputs are invalid they are asked again
while invalid_input:
start()
#if wrong file is given as input it prints as below
else:
print("File not recognized. Please provide a valid CONVERGE output file")
#if file is not found it prints as below
except:
print("File not found")
Explanation:-
- The required libraries are imported and empty arrays of all quanties are created.
- Using 'input' function, the link of the file to be parsed is obtained.
- Then function for user input and plotting is written.
- Try and except blocks are used to check whether any such file exists in the given location. If the file does not exist, then except block displays the error message.
- Converge file has its name and the version along with the date as the opening line. The file is opened in 'read' mode and the first line is split using 'split' command, that splits the string at whitespace and stored in a. If the second element of 'a' is not 'CONVERGE', then 'else' block displays error.
- We can observe that the value of properties is not preceded by '#'. Thus we use if loop to avoid lines that start with '#'. Lines are read and split using 'split' command. The list containing the value of properties gets appended when the value from the subsequent line is read.
- Each property is listed and its respective column number is obtained from the user to plot against each other.
- Column numbers of the properties to be plot is passed as the argument of the function 'user_input', which draws the graph along with the x-axis and y-axis labels obtained from the third line of the file.
- The area under the pressure-volume graph gives the work obtained from the engine. We use 'trapz' function to integrate the PV diagram. It works on the principle of the trapezoidal rule by approximating the region under the graph of the function as a trapezoid and calculating its area.
- We calculate the power and specific consumption of fuel.
Output:-
1. After running the program it asks the user for the file path
2. If the invalid path is given it shows as below
3. After entering the file path it displays properties and the column no. and asks user to enter no. for which the plot should be plotted.
4. If the invalid no.s are entered it asks to enter again valid no.s. and then calculates work, power & sfc.
5. The plot for pressure and volume is generated as below.
Thus, work done by the engine is 0.500587 kJ, Power is 6.257333 kW, Specific fuel consumption is 0.143831 g/kWh.