Challenge

Week 6 - Data analysis

AIM: Data analysis using Python

OBJECTIVES:

1. Extract information the user requires and save the same as an image.

2. Display Power, work and bsfc.

3. Check whether the input file is correct or not.

Compatibility Check: First the program accepts the file, then it will check whether the file is of correct format using 'try' if it's not as expected then, 'except' will throw an error and the program ceases.

Data Visulaization: The program asks the user to enter the parameters of which the plot is to be plotted.

Performance Calculation: Work done is area under the PV diagram. The area is calculated using trapezoidal rule. Also, bsfc and Power is calculated using standard formulas. N=1500 RPM and fuel consumed is 20 micrograms of fuel per cycle.

import sys
import math
import matplotlib.pyplot as plt
import numpy as np


#Opening the file and compatibility check

try:

  f=open('engine_data.out','r') # if correct file is provided it will open else except will run
except FileNotFoundError: # if the file is not expected it will throw an error and will exit the program
  print('File not recognized. Please provide a valid CONVERGE output file!')
  sys.exit(1)


value=[]

#reading from file and assigning values to arrays 

for line in f: # data extraction for all the lines is being done and it is being stored in Value array
      if '#' not in line :
       values=line.split()
       
       value.append(values)   
col1=np.transpose(value) # the data collected is stored in rows, it is converted into columns
col1=col1.astype(np.float)


#Area under PV Diagram 

xvar=col1[1] #pressure


yvar=col1[7] #volume


dx=[]
k=0 # Using trapezoidal rule we will find the area enclosed within PV diagram
while k < 8664 : 
  
  dif=xvar[k+1]-xvar[k] #pressure difference is appended
  dx.append(dif)
  
  k=k+1


W=[]
Work=0
t=0
while t < 8664 : # area is calculated between two positions
   
   W.append(((dx[t])/2)*(yvar[0]+2*(yvar[t+1])+yvar[8664]))

   

   t=t+1


for n in range(0,len(W)): # complete area is calculated
   Work=Work+W[n]


#Work,Power and Specific fuel consumption calculation and output

Work=Work*1000000
print('Work Output in Joules:')
print(abs(Work))

print('Power Output in Watts:')
power=(Work*2*1500*math.pi*0.001)/60 # speed given is 1500 RPM
print(abs(power))

print('Specific fuel consumption in gram/Joule:')
bsfc=0.02/abs(power) # 20 micrograms = 0.02 kg
print(bsfc)

#name is used for printing the labels for X and Y axis in the plots
name=['Crank','Pressure','Max_Pres','Min_Pres','Mean_Temp','Max_Temp','Min_Temp','Volume','Mass','Density','Integrated_HR','HR_Rate','C_p','C_v','Gamma','Kin_Visc','Dyn_Visc']

print('Press \n 1 for Crank \n 2 for Pressure \n 3 for Max_Pres \n 4 for Min_Pres \n 5 for Mean_Temp \n 6 for Max_Temp \n 7 for Min_Temp \n 8 for Volume \n 9 for Mass \n 10 for Density \n 11 for Integrated_HR \n 12 for HR_Rate \n 13 for C_p \n 14 for C_v \n 15 for Gamma \n 16 for Kin_Visc \n 17 for Dyn_Visc')

#user enters the number of column for X and Y axes
x=int(input('Enter number corresponding to value you want to plot on X-axis\n'))

y=int(input('Enter number corresponding to value you want to plot on Y-axis\n'))

#PLOTTING the Inputs provided by the user

plt.figure(1)
plt.plot(col1[x-1],col1[y-1])
plt.title(name[y-1]+'  &  '+name[x-1]+'     Plot',fontsize=15,color='red')
plt.xlabel(name[x-1],fontsize=8)
plt.ylabel(name[y-1],fontsize=8)
plt.savefig(name[x-1] + ' vs ' + name[y-1] + '.png') # saving the plot with a name
plt.show()


#Plotting PV diagram

plt.figure(2)
plt.plot(col1[7],col1[1])
plt.title('P-V diagram',fontsize=15,color='red')
plt.xlabel('Volume',fontsize=8)
plt.ylabel('Pressure',fontsize=8)
plt.show()
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.