Week 6 - Data analysis
import numpy as np
import matplotlib.pyplot as plt
def check_file_compatibility(file_path):
"""
Check if the file is a valid CONVERGE output file.
Return True if compatible, False otherwise.
"""
try:
with open(file_path, 'r') as file:
header = file.readline().strip()
if header.startswith('# CONVERGE Release Build'):
return True
else:
print("File not recognized. Please provide a valid CONVERGE output file.")
return False
except:
print("Error reading the file. Please check the file path.")
return False
def plot_columns(file_path, columns):
"""
Plot the specified columns and save them as individual image files.
"""
data = np.loadtxt(file_path, skiprows=9) # Assuming the data starts from row 10
column_labels = ["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"]
for col in columns:
plt.figure()
plt.plot(data[:, 0], data[:, col])
plt.title(column_labels[col])
plt.xlabel(column_labels[0])
plt.ylabel(column_labels[col])
plt.savefig(f"{column_labels[col]}.png")
def calculate_performance(file_path):
"""
Calculate the area under the P-V diagram, power output, and specific fuel consumption.
"""
data = np.loadtxt(file_path, skiprows=9)
pressure = data[:, 1]
volume = data[:, 7]
# Calculate the area under the P-V diagram
area_under_pv = np.trapz(pressure, volume)
# Calculate the power output (assuming 1500 RPM)
rpm = 1500
power = area_under_pv * (rpm / 60)
# Calculate the specific fuel consumption (assuming 20 micrograms per stroke cycle)
fuel_consumption = 20e-6 # 20 micrograms per stroke cycle
specific_fuel_consumption = fuel_consumption / power
return area_under_pv, power, specific_fuel_consumption
# Example usage
file_path = "C:\\Users\\joelg\\Downloads\\engine_data (3).out"
if check_file_compatibility(file_path):
plot_columns(file_path, [1, 2, 7, 8]) # Plot columns 1, 2, 7, and 8
area_under_pv, power, specific_fuel_consumption = calculate_performance(file_path)
print(f"Area under P-V diagram: {area_under_pv:.2f}")
print(f"Power output: {power:.2f} W")
print(f"Specific fuel consumption: {specific_fuel_consumption:.2f} kg/W-s")
The area under P-V diagram: 0.00 Power output: 0.01 W Specific fuel consumption: 0.00 kg/W-s