Week 6 - Data analysis
Objective: To extract the data from the output file and plot graphs.
Problem statement:
1. Data visualizer
- Script should take column numbers as the input and plot the respective columns.
- Each file should be saved by the name of the column.
- The plot labels should be extracted from the file and must appear as labels in the plots.
2. Compatibility check
- Code should exit gracefully, if a non-compatible file is provided as an input.
3. Basic performance calculation
- 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.
Solution Procedure:
1. Data visualizer
- Required libraries are imported.
- User is asked to eter the file name from which data has to be extracted.
- File is opened.
- Column numbers for x-axis and y-axis data are asked.
- Program enters a 'for' loop to extract the data.
- An 'if' statement is used to extract column labels.
- Another 'elif' statement is used to extract the data for each column and converted into 'float' class.
- Graph is plotted and saved by the column name.
2. Compatibility check
- Here 'try-except' statement is used to check if the file name is correct or not.
- If the file name is correct then the program moves to the next step.
- If the file name is incorrect, the program exits gracefully with a custom error message.
3. Basic performance calculation
- Area under P-V diagram is calculated only in case the volume (column 8) is chosen for x-axis and pressure (column 2) is chosen for y-axis.
- This is achieved using an 'if' statement.
- Area or Work is calculated using formula: area = P * change in volume.
- P is taken as average of two consecutive pressure values and change in volume is calculated by taking difference between two consecutive volume values.
- Power is calculated using formula: power = work * speed(in rad/s)
- Specific fuel consumption is calculated by dividing fuel consumed per second by power generated.
Results:
- The plots are successfully seved by the column names.
Discussion:
- The calculations given in basic performance calculations can only be perdormed using pressure and volume data therefore the calculated values are displayed only when column 8 (volume) is selected for x-axis and column 2 (pressure) is selected for y-axis.
Python code:
import matplotlib.pyplot as plt
import math
# initaliization
x_label = []
y_label = []
x_column = []
y_column = []
# asking the file name
print('Enter the file name containing the data:\n')
name = input() # taking file name as input and storing it in 'name' variable
try: # try-except statement is used to modify the file name error message
file = open(name) # opening the file to extract data
print('Please enter column number of data for x-axis of the plot:\n')
x = int(input()) # taking input of column number for x axis
print('Please enter column number of data for y-axis of the plot:\n')
y = int(input()) # taking input of column number for y axis
line_count = 1 # setting initial line count as 1
for line in file:
if line_count == 3: # using if statement to extract the plot labels
x_label = line.split()[x]
y_label = line.split()[y]
elif '#' not in line: # using elif statement to extract the contents of x and y column
x_column.append(float(line.split()[x-1]))
y_column.append(float(line.split()[y-1]))
line_count = line_count + 1
# caluclation of area under P-V diagram
if (x == 8) and (y ==2): # area can only we calculated when we have pressure and volume data
work = 0 # assigning initial work as zero
for i in range(len(x_column)-1):
p_avg = ((y_column[i+1] + y_column[i])/2) * pow(10,6) # calculating average of two consecutive pressure values and converting to pascal from mega pascal
v = x_column[i+1] - x_column[i] # calculating change in volume
work = work + (p_avg * v) # area or work = p * change in volume
i = i+1
print('Area under P-V curve (work done) is ' + str(work) + ' joules\n')
# power calculation
speed = (1500 * 2 * math.pi) / 60 # converting speed from RPM to rad/sec
power = work * speed # power = work * speed in rad/sec
print('Power output of the engine is ' + str(power) + ' watts\n')
# specific fuel consumption calculation
m_fuel = (20 * pow(10,-6) * speed) / (4 * math.pi) # converting mass of fuel consumed to grams/sec
sfc = m_fuel/power # specific fuel consumption = mass of fuel consumed / power generated
print('Specific fuel consumption of engine is ' + str(sfc) + ' grams/joules\n')
# saving the plot
plt.figure()
plt.plot(x_column,y_column)
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.title(y_label + ' vs ' + x_label + ' Plot')
plt.savefig(y_label + '_vs_' + x_label + '.png')
except FileNotFoundError:
print('File not recognized. Please provide a valid CONVERGE output file') # custom error message for incorrect file name
Plot:
Screenshots:
1. Comapatibility check
2. Performance values