Project 1 - Parsing NASA thermodynamic data
Objective: Develop a Matlab code to parse NASA's thermodynamic data file and compute the thermodynamic properties for different gas species.
1. Create a function to extract and process the 14 coefficients, calculating enthalpy, entropy, and specific heats from the file "Thermo.dat" following the NASA polynomial format.
2. Determine and display the molecular weight of each gas species in the command window.
3. Generate plots for Cp, Enthalpy, and Entropy based on the local temperature range for each species (from low to high temperature).
4. Save the generated plots as images with appropriate names, organizing them into separate folders for each species automatically.
File Parsing Steps:
1. Define the function for file parsing.
2. Create an InputParser object.
3. Add inputs to the parsing scheme.
4. Adjust parsing properties as needed.
5. Parse the inputs.
6. Utilize the parsed inputs within the function.
7. Call the function for processing.
NASA Thermodynamic File:
The "Thermo.dat" file contains 14 coefficients for 53 elements provided by NASA, which are utilized to compute specific heat, enthalpy, and entropy for each element over its respective temperature range.
List of 53 Elements in 'Thermo.dat' file:
AR, C, C2H, C2H2, C2H3, C2H4, C2H5, C2H6, C3H7, C3H8, CH, CH2, CH2(S), CH2CHO, CH2CO, CH2O, CH2OH, CH3, CH3CHO, CH3O, CH3OH, CH4, CN, CO, CO2, H, H2, H2CN, H2O, H2O2, HCCO, CCOH, HCN, HCNN, HCNO, HCO, HNCO, HNO, HO2, HOCN, N, N2, N2O, NCO, NH, NH2, NH3, NNH, NO, NO2, O, O2, OH.
Enthalpy, Entropy, and Specific Heat:
These properties are crucial for analyzing thermodynamic processes like combustion, refrigeration, chemical kinetics, and more. NASA provides polynomials to calculate Cp, H, and S using coefficients in the thermodynamic file, distinguished by high-temperature and low-temperature ranges.
Coding Solution:
First, we have to build specific functions for the above-mentioned parameters and then start developing the "main" part of the code.
Paraphrased MATLAB code for specific heat, entropy, enthalpy, and molecular weight calculations:
1. Specific Heat Calculation:
Specific heat refers to the energy required to raise the temperature of a substance by a certain amount.
function Cp = SpecificHeat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,global_midtemp)
%calculating specific_heat for higher and lower temperature respectively
if(T>= global_midtemp)
Cp=R*(a1+(a2*T)+(a3*(T).^2)+(a4*(T).^3)+(a5*(T).^4));
else
Cp=R*(a8+(a9*T)+(a10*(T).^2)+(a11*(T).^3)+(a12*(T).^4));
end
end
2. Entropy Calculation:
Entropy measures a system's thermal energy that cannot be used for work, reflecting its disorder or randomness.
function S=EntropyCalc(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,global_midtemp)
%calculating entropy for higher and lower temperature respectively
if(T>= global_midtemp)
S=R*(a1.*log(T)+(a2*T)+((a3*(T).^2)/2)+((a4*(T).^3)/3)+((a5*(T).^4)/5)+a7);
else
S=R*(a8.*log(T)+(a9*T)+((a10*(T).^2)/2)+((a11*(T).^3)/3)+((a12*(T).^4)/4));
end
end
3. Enthalpy Calculation:
Enthalpy combines a system's internal energy with pressure-volume effects.
function H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,global_midtemp)
%calculating enthalpy for higher and lower temperature respectively
if(T>=global_midtemp)
H=R*((a1*T)+((a2*(T).^2)/2)+((a3*(T).^3)/3)+((a4*(T).^4)/4)+((a5*(T).^5)/5)+a6);
else
H=R*((a8*T)+((a9*(T).^2)/2)+((a10*(T).^3)/3)+((a11*(T).^4)/4)+((a12*(T).^5)/5)+a13);
end
end
4. Molecular Weight Calculation:
Molecular weight is determined based on the elements present in a species.
function Molecular_weight=wt(species_name)
Elements=['H' 'C' 'O' 'N' 'A' 'S'];
Atomic_weight =[1 12 16 14 40 32];
species=upper(species_name)
Molecular_weight=0;
for i=1:length(species)
for j=1:length(Elements)
if strcmp(species(i),Elements(j))
Molecular_weight=Molecular_weight+Atomic_weight(j);
k=j;
else
n=str2double(species(i));
if (n>1)
Molecular_weight=Molecular_weight+Atomic_weight(k)*(n-1);
break
end
end
end
end
end
The main code involves reading data from a file, extracting coefficients, and using these functions to calculate specific heat, entropy, enthalpy, and molecular weight for different species, along with plotting and saving relevant information.
clear all
close all
clc
%R=molar gas constant
R = 8.314;
%syntax to open the file THERMO.dat
f1 = fopen('THERMO.dat', 'r');
% To read lines
l1 = fgetl(f1);
l2 = fgetl(f1);
value = strsplit(l2, ' ');
% To get global temperatures from file in number form
global_lowtemp = str2num(value{2});
global_midtemp = str2num(value{3});
global_hightemp = str2num(value{4});
% temperature range
T = linspace(global_lowtemp, global_hightemp, 1000);
% skipping these 3 lines
l3 = fgetl(f1);
l4 = fgetl(f1);
l5 = fgetl(f1);
% Reading species, calculating cp, h, s, and plotting
for i = 1:53
tline = fgetl(f1);
A = strsplit(tline, ' ');
name_of_species = (A{1});
lowtemp = str2num(A{end-3});
midtemp = str2num(A{end-2});
hightemp = str2num(A{end-1});
% Finding 7 higher coefficients and 7 lower coefficients
tline1 = fgetl(f1);
a = findstr(tline1, 'E');
a1 = tline1(1:a(1)+3);
a1 = str2num(a1);
a2 = tline1(a(1)+4:a(2)+3);
a2 = str2num(a2);
a3 = tline1(a(2)+4:a(3)+3);
a3 = str2num(a3);
a4 = tline1(a(3)+4:a(4)+3);
a4 = str2num(a4);
a5 = tline1(a(4)+4:a(5)+3);
a5 = str2num(a5);
tline2 = fgetl(f1);
a = findstr(tline2, 'E');
a6 = tline2(1:a(1)+3);
a6 = str2num(a6);
a7 = tline2(a(1)+4:a(2)+3);
a7 = str2num(a7);
a8 = tline2(a(2)+4:a(3)+3);
a8 = str2num(a8);
a9 = tline2(a(3)+4:a(4)+3);
a9 = str2num(a9);
a10 = tline2(a(4)+4:a(5)+3);
a10 = str2num(a10);
tline3 = fgetl(f1);
a = findstr(tline3, 'E');
a11 = tline3(1:a(1)+3);
a11 = str2num(a11);
a12 = tline3(a(1)+4:a(2)+3);
a12 = str2num(a12);
a13 = tline3(a(2)+4:a(3)+3);
a13 = str2num(a13);
a14 = tline3(a(3)+4:a(4)+3);
a14 = str2num(a14);
% calculate specific heat (cp), Enthalpy (H), Entropy (S)
Cp = SpecificHeat(a1, a2, a3, a4, a5, a8, a9, a10, a11, a12, T, R, global_midtemp);
H = Enthalpy(a1, a2, a3, a4, a5, a6, a8, a9, a10, a11, a12, a13, T, R, global_midtemp);
S = EntropyCalc(a1, a2, a3, a4, a5, a7, a8, a9, a10, a11, a12, a14, T, R, global_midtemp);
% calculating molecular weight and printing it.
Molecular_weight = MolecularWeightCalc(name_of_species);
fprintf('molecular weight of %s= %f\n', name_of_species, Molecular_weight);
% PLOTTING CP, H, S
cd('/Users/shreyashpanchal/Desktop/nasa_output')
% creating the folder
mkdir(name_of_species);
cd(name_of_species);
% plotting T vs cp
figure(1)
plot(T, Cp, 'linewidth', 5, 'color', 'r')
xlabel('Temperature(T)')
ylabel('Specific heat(cp)')
grid on
title(name_of_species)
saveas(figure(1), 'specific heat.jpg');
% Plotting T vs H
figure(2)
plot(T, H, 'linewidth', 5, 'color', 'b')
xlabel('Temperature(T)')
ylabel('Enthalpy(H)')
grid on
title(name_of_species)
saveas(figure(2), 'Enthalpy.jpg');
% Plotting T vs S
figure(3)
plot(T, S, 'linewidth', 5, 'color', 'y')
xlabel('Temperature(T)')
ylabel('Entropy(S)')
grid on
title(name_of_species)
saveas(figure(3), 'Entropy.jpg');
cd('/Users/shreyashpanchal/Desktop/nasa_output')
end
Code Explanation:
- File Handling and Initialization:
- The code starts by clearing the workspace, closing any open figures, and clearing the command window.
- It creates a new directory on the desktop to store the output files.
- Reading Data from File:
- The code opens the "THERMO.dat" file and reads header information to obtain the global temperature range.
- Coefficient Extraction:
- It skips three comment lines in the file and then loops through each species in the data file.
- For each species, it reads and extracts the coefficients for specific heat, enthalpy, and entropy from three consecutive lines in the file.
- Calculation Functions:
- It uses separate functions for specific heat, enthalpy, and entropy calculations based on the extracted coefficients and the given temperature range.
- The specific heat, enthalpy, and entropy calculations are performed using mathematical formulas.
- Molecular Weight Calculation:
- The code calculates the molecular weight of each species based on its atomic composition.
- It writes the molecular weight information to a text file.
- Plotting and Saving:
- For each species, the code creates a directory and saves plots of specific heat, enthalpy, and entropy vs. temperature in that directory.
- The plots are customized with appropriate labels, titles, and grid lines.
- Closing File and Cleanup:
- Finally, the code closes the data file after processing all species.
- It also includes a helper function to extract coefficients from the file lines.
This code essentially reads NASA thermodynamic data, calculates thermodynamic properties for various gas species, computes molecular weights, and saves results and plots in organized folders.
Outputs:
The output plots for all the species are presented in the Google Drive folder. I have mentioned a couple of plots below just for reference.
Folder Link: https://drive.google.com/drive/folders/1wI-igoe0NnPeckqkwvHJYp7pd92XZrf6?usp=sharing
Sample Plots:



Command Line Window:
species =
'O'
molecular weight of O= 16.000000
species =
'O2'
molecular weight of O2= 32.000000
species =
'H'
molecular weight of H= 1.000000
species =
'H2'
molecular weight of H2= 2.000000
species =
'OH'
molecular weight of OH= 17.000000
species =
'H2O'
molecular weight of H2O= 18.000000
species =
'HO2'
molecular weight of HO2= 33.000000
species =
'H2O2'
molecular weight of H2O2= 34.000000
species =
'C'
molecular weight of C= 12.000000
species =
'CH'
molecular weight of CH= 13.000000
species =
'CH2'
molecular weight of CH2= 14.000000
species =
'CH2(S)'
molecular weight of CH2(S)= 46.000000
species =
'CH3'
molecular weight of CH3= 15.000000
species =
'CH4'
molecular weight of CH4= 16.000000
species =
'CO'
molecular weight of CO= 28.000000
species =
'CO2'
molecular weight of CO2= 44.000000
species =
'HCO'
molecular weight of HCO= 29.000000
species =
'CH2O'
molecular weight of CH2O= 30.000000
species =
'CH2OH'
molecular weight of CH2OH= 31.000000
species =
'CH3O'
molecular weight of CH3O= 31.000000
species =
'CH3OH'
molecular weight of CH3OH= 32.000000
species =
'C2H'
molecular weight of C2H= 25.000000
species =
'C2H2'
molecular weight of C2H2= 26.000000
species =
'C2H3'
molecular weight of C2H3= 27.000000
species =
'C2H4'
molecular weight of C2H4= 28.000000
species =
'C2H5'
molecular weight of C2H5= 29.000000
species =
'C2H6'
molecular weight of C2H6= 30.000000
species =
'CH2CO'
molecular weight of CH2CO= 42.000000
species =
'HCCO'
molecular weight of HCCO= 41.000000
species =
'HCCOH'
molecular weight of HCCOH= 42.000000
species =
'H2CN'
molecular weight of H2CN= 28.000000
species =
'HCN'
molecular weight of HCN= 27.000000
species =
'HNO'
molecular weight of HNO= 31.000000
species =
'N'
molecular weight of N= 14.000000
species =
'NNH'
molecular weight of NNH= 29.000000
species =
'N2O'
molecular weight of N2O= 44.000000
species =
'NH'
molecular weight of NH= 15.000000
species =
'NH2'
molecular weight of NH2= 16.000000
species =
'NH3'
molecular weight of NH3= 17.000000
species =
'NO'
molecular weight of NO= 30.000000
species =
'NO2'
molecular weight of NO2= 46.000000
species =
'HCNO'
molecular weight of HCNO= 43.000000
species =
'HOCN'
molecular weight of HOCN= 43.000000
species =
'HNCO'
molecular weight of HNCO= 43.000000
species =
'NCO'
molecular weight of NCO= 42.000000
species =
'CN'
molecular weight of CN= 26.000000
species =
'HCNN'
molecular weight of HCNN= 41.000000
species =
'N2'
molecular weight of N2= 28.000000
species =
'AR'
molecular weight of AR= 40.000000
species =
'C3H8'
molecular weight of C3H8= 44.000000
species =
'C3H7'
molecular weight of C3H7= 43.000000
species =
'CH3CHO'
molecular weight of CH3CHO= 44.000000
species =
'CH2CHO'
molecular weight of CH2CHO= 43.000000
>>
.