Project 1 - Parsing NASA thermodynamic data
AIM:- Write a code in Matlab to parse the NASA thermodynamic data file and then calculate the thermodynamic properties of various gas species.
OBJECTIVE:-
- Write a function that extracts the 14 coefficients and calculates the enthalpy, entropy, and specific heats for all the species in the data file.
- Calculate the molecular weight of each species and display it in the command window.
- Plot the Cp, Enthalpy, and Entropy for the local temperature range (low temperature: high temperature) specific for each species.
- Save the plots as images with appropriate names and dump them in separate folders for each species with a suitable name. All these should be generated automatically.
- The main program that you will submit will just take the "THERMO.dat" file as an input and generate Cp, H, and S plots.
- Screenshot of the window where all the folders are saved.
- Plots for o2, N2, and Co2 species.
THEORY:-
File parsing: File parsing is the process of analyzing a string symbol in natural language computer languages or data structures, conforming to the rules of formal grammar. The term parsing comes from Latin pars(orationis), meaning part (of speech). Within computational linguistics the term is used to refer to the formal analysis by a computer of a sentence or other string of words into its constituents, resulting in a parse tree showing their syntactic relation to each other, which may also contain semantic and other information. Some parsing algorithms may generate a parse forest or list of parse trees for a syntactically ambiguous input.
Parsing a file means reading in a data stream of some sort and building an in-memory model of the semantic content of that data. This aims at facilitating performing some kind of transformation on the data. This aims at facilitating performing some kind of transformation on the data.
Enthalpy: Enthalpy is a thermodynamic property of a system. It is the sum of the internal energy added to the product of the pressure and volume of the system. It reflects the capacity to do non-mechanical work and the capacity to release heat.

Entropy: Enthalpy, a property of a thermodynamic system, is equal to the system's internal energy plus the product of its pressure and volume. In a system contained so as to prevent the mass transfer, for processes at constant pressure, the heat absorbed or released equals the change in enthalpy.

Specific heat: Specific heat is the ratio of the quantity of heat required to raise the temperature of a body by 1 degree to that required to raise the temperature of an equal mass of water by one degree.

Main code:
clear all
close all
clc
%getting data file
f=fopen('THERMO.dat','r');
%skip the first line in data
fgetl(f);
R=8.314;%gass constant
%reading header information
tline=fgetl(f);
A=str2num(tline);
global_low_temp=A(:,1);
global_mid_temp=A(:,2);
global_high_temp=A(:,3);
%skipping comment lines
for i=1:3
fgetl(f);
end
%reading the species data
for i=1:53
line_1=fgetl(f); %To read 1st line of species,as ranked 1 on RHS of data file
L1=strsplit(line_1); % To split 1st line
species=L1{1}; % To extract Species from 1st line
local_low_temp=str2num(L1{end-3}); % To extract LOCAL Low Temperature
local_high_temp=str2num(L1{end-2}); % To extract LOCAL High Temperature
local_mid_temp=str2num(L1{end-1}); % To extract LOCAL medium Temperature
T = linspace(local_low_temp,local_high_temp,500); % To set a range of Local Temperature
line_2=fgetl(f); %To read 2nd line of species,
L2=strfind(line_2,'E'); % To find the position of E in 2nd line
A1=str2num(line_2(1:L2(1)+3)); % To extract 1st coefficient
A2=str2num(line_2(L2(1)+4:L2(2)+3)); % To extract 1st coefficient
A3=str2num(line_2(L2(2)+4:L2(3)+3));
A4=str2num(line_2(L2(3)+4:L2(4)+3));
A5=str2num(line_2(L2(4)+4:L2(5)+3));
line_3=fgetl(f); %To read 3rd line of species,
L3=strfind(line_3,'E'); % To find the position of E in 4th line
A6=str2num(line_3(1:L3(1)+3)); % To extract coefficient
A7=str2num(line_3(L3(1)+4:L3(2)+3));
A8=str2num(line_3(L3(2)+4:L3(3)+3));
A9=str2num(line_3(L3(3)+4:L3(4)+3)) ;
A10=str2num(line_3(L3(4)+4:L3(5)+3)) ;
line_4=fgetl(f); %To read 4th line of species,
L4=strfind(line_4,'E'); % To find the position of E in 4th line
A11=str2num(line_4(1:L4(1)+3)); % To extract coefficient
A12=str2num(line_4(L4(1)+4:L4(2)+3));
A13=str2num(line_4(L4(2)+4:L4(3)+3));
A14=str2num(line_4(L4(3)+4:L4(4)+3));
for j=1:length(T)
if T(j)>= global_mid_temp
% for lower temperature
Cp(j) = (A1+ (A2.*T(j)) + (A3.*T(j).^2) + (A4.*T(j).^3) + (A5.*T(j).^4)) * R ;
H(j) = (A1+(A2.*(T(j)./2))+(A3.*(T(j).^2)./3)+(A4.*(T(j).^3)./4)+(A5.*(T(j).^4)./5)+(A6./T(j))).*(T(i).*R);
S(j) = ((A1.* log(T(j)))+(A2.*T(j))+(A3.*T(j).^2)+(A4.*T(j).^3)+(A5.*T(j).^4)+A7)* R ;
else
%for higher temperature
Cp(j) =(A8+ (A9.*T(j)) + (A10.*T(j).^2) + (A11.*T(j).^3) + (A12.*T(j).^4)) * R ;
H(j) = (A8+(A9.*(T(j)./2))+(A10.*(T(j).^2)./3)+(A11.*(T(j).^3)./4)+(A12.*(T(j).^4)./5)+(A13./T(j))).*(T(j).*R);
S(j) = ((A8.* log(T(j)))+(A9.*T(j))+(A10 *T(j).^2)+(A11.*T(j).^3)+(A12.*T(j).^4)+A13)* R ;
end
end
mkdir(['C:\Users\punit\Desktop\matlab\project1\plots\',species])
figure(1);
plot(T,Cp)
grid on
ylabel('specific heat')
xlabel('local temperature')
title('specific heat vs local temp')
% fig1=sprintf("Cp vs T of %s.png",species); % To give a name of the file
% saveas(gca,fig1); % To save the image file
figure(2);
plot(T,H)
grid on
ylabel('Entalpy')
xlabel('local temperature')
title('Entalpy vs local temp')
% fig2=sprintf("H vs T of %s.png",species);
% saveas(gca,fig2);
figure(3);
plot(T,S)
grid on
ylabel('Entropy')
xlabel('local temperature')
title('Entropy vs local temp')
% fig3=sprintf("Cp vs T of %s.png",species);
% saveas(gca,fig3);
cd(['C:\Users\punit\Desktop\matlab\project1\plots\',species])
saveas(1,'Cp vs T','jpg')
saveas(2,'H vs T','jpg')
saveas(3,'S vs T','jpg')
%claculation of molecular weight
atom=['H','C','O','N','A',];
atomic_weight=[1,12,14,14,39];
mw=0;
for p=1:length(species)
for q=1:length(atomic_weight)
if strcmp(species(p),atom(q))
mw=mw+atomic_weight(q);
position=q;
end
end
num=str2num(species(p));
if (num)
mw=mw+(atomic_weight(position)*(num-1));
fprintf('Molecular Weight of %s :- ',species);
fprintf('%f',mw);
disp(' ')
end
end
end
Explanation of code:
- The first step is to clear all, close all, clc. The clear all command helps to clear the workspace. The close all command closes all figures and the clc command clears the command window.
- The fopen command is used in reading mode to read the THERMO.dat file.
- The temperature values are calculated by using strsplit,str2num commands.
- Then the comment lines are stored in three variables i.e., line3, line4, line5.
- The for loop is used to create an array that contains every line of the file.
- Then the coefficients are founded by using the findstr, str2num.
- Then the directory is created to save the plots in png format with the species name.
- The cp, h, s functions are called to the main program which has been created separate script.
- Then the plots are created between temperature and cp, s, h.
- The saves are used to save the plots in png extension.
- Finally, the mol_wt function is called for calculating the molecular weight of the species.
- The fclose command is used to end the reading of the file.
Molecular weight command window:-
Molecular Weight of O2 :- 28.000000
Molecular Weight of H2 :- 2.000000
Molecular Weight of H2O :- 2.000000
Molecular Weight of HO2 :- 29.000000
Molecular Weight of H2O2 :- 2.000000
Molecular Weight of H2O2 :- 30.000000
Molecular Weight of CH2 :- 14.000000
Molecular Weight of CH2(S) :- 14.000000
Molecular Weight of CH3 :- 15.000000
Molecular Weight of CH4 :- 16.000000
Molecular Weight of CO2 :- 40.000000
Molecular Weight of CH2O :- 14.000000
Molecular Weight of CH2OH :- 14.000000
Molecular Weight of CH3O :- 15.000000
Molecular Weight of CH3OH :- 15.000000
Molecular Weight of C2H :- 24.000000
Molecular Weight of C2H2 :- 24.000000
Molecular Weight of C2H2 :- 26.000000
Molecular Weight of C2H3 :- 24.000000
Molecular Weight of C2H3 :- 27.000000
Molecular Weight of C2H4 :- 24.000000
Molecular Weight of C2H4 :- 28.000000
Molecular Weight of C2H5 :- 24.000000
Molecular Weight of C2H5 :- 29.000000
Molecular Weight of C2H6 :- 24.000000
Molecular Weight of C2H6 :- 30.000000
Molecular Weight of CH2CO :- 14.000000
Molecular Weight of H2CN :- 2.000000
Molecular Weight of N2O :- 28.000000
Molecular Weight of NH2 :- 16.000000
Molecular Weight of NH3 :- 17.000000
Molecular Weight of NO2 :- 42.000000
Molecular Weight of N2 :- 28.000000
Molecular Weight of C3H8 :- 36.000000
Molecular Weight of C3H8 :- 44.000000
Molecular Weight of C3H7 :- 36.000000
Molecular Weight of C3H7 :- 43.000000
Molecular Weight of CH3CHO :- 15.000000
Molecular Weight of CH2CHO :- 14.000000
>>


PLOT for O2



PLOT of N2



PLOT of CO2


Screenshot of the window where all the folders are saved:-

Conclusion:-
Using MATLAB code, the necessary coefficients required to evaluate various thermodynamic parameters for different species are extracted from NASA thermodynamic data file. The specific heat, enthalpy, and entropy for all the species are calculated and plotted against the local temperature range. The molecular weight is calculated for all the species.