Project 1 - Parsing NASA thermodynamic data
AIM:
To write a matlab code tp parse the NASA thermodynamic data file and then calculate thermodynamic properties of various gas species.
OBJECTIVE :
- Write a function that extracts the 14 co-effecients and calculate the enthalpy,entropy and specific heats for for all the species in the data file.
- plot the Cp , Enthalpy and Entropy for the local temperature (low temperature : high temperature ) specific for each species
- Save the plots as images with names and dump them in separate folders for each species with suitable. 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.
File Parsing :
parsing, syntax analysis or syntatic analysis is the process of analysing the string of synmbols either in natural or computer language.
parsing a file means reading in a data stream of some sort and building an in memory model of the semantic content of the data.
NASA Thermodynamic File:
NASA thermodynamics file which is THERMO.dat file in which there are temperature range and 53 species are given in these data and eacg species consists of 14 coefficients and temperature ranges and we have to parse these 14 coefficients to find Specific heat , Entropy and Enthalpy using formula as :
WHERE:
R = Universal Gas constant
T = Temperature
Cp = Specific heat (K)
S = Entropy (KJ/mol-K)
H = Enthalpy(KJ/mol)
a(i) = Coefficients where , i = 1 to 14
Specific Heat :
The specific heat is the amount of heat per unit mass required raise the temperature by one degree celsius.
Cp = R*(a1+(a2*T)+(a3*(T).^2)+(a4*(T).^3)+(a5*(T).^4))
Entropy :
Entropy is the measure of systems thermal energy per unit temperature that is unavailable for doing useful work. because the work is obtained from molecular motion, the amount of molecular disorder is the measure of entropy .
S = R*(a1.*log(T) +(a2*T)+((a3*(T).^2)/2)+((a4*(T).^3/3)+((a5*(T).^4/4)+a7)

Enthalpy :
Enthalpy, a property of a thermodynamic system which 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 .
H = R*T.(a1=((a2*T)/2)+((a3*(T).^2)/3)+ ((a4*(T).^3)/4)+((a5*(T).^4)/5)+((a6.T)))

FUNCTION FOR SPECIFIC HEAT:
%Specific heat calculation function
function Cp = specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,temp,R,temp_m)
%comparing with global temperature
%if less than global temperature- minimum coefficients are taken
if temp > temp_m
Cp =R*(a1+(a2*temp)+(a3*(temp).^2) +(a4*(temp).^3)+(a5*(temp).^4));
else
Cp =R*(a8+(a9*temp)+(a10*(temp).^2) +(a11*(temp).^3)+(a12*(temp).^4));
%else maximum coefficients are taken
end
FUNCTION FOR ENTHALPY
% enthalpy function calculation
function H = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,temp,R,temp_m)
%comparing with local temperatures
%if greater than local temperature - maximum coefficients are taken
if (temp > temp_m )
H = R*temp.*(a1 + ((a2*temp)/2)+ ((a3*(temp).^2)/3)+ ((a4*(temp).^3)/4) +((a5*(temp).^4)/5) + ((a6./temp)));
else
H = R*temp.*(a8+((a9*temp)/2)+ ((a10*(temp).^2)/3)+((a11*(temp).^3)/4)+((a12*(temp).^4)/5)+((a13./temp)));
%else minimum coefficients are taken
end
FUNCTION FOR ENTROPY :
%Entropy calculation function
function S = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,temp,R,temp_m);
%comparing with local temperature
%if less than global temperature - minimum coefficients are taken
if (temp > temp_m)
S = R*(a1.*log(temp) + (a2*temp) + ((a3*(temp).^2)/2) + ((a4*(temp).^3)/3)+((a5*(temp).^4)/4)+ a7);
else
S = R*(a8.*log(temp) + (a9*temp) + ((a10*(temp).^2)/2) + ((a11*(temp).^3)/3)+((a12*(temp).^4)/4)+ a14);
%maximum coefficients are taken
end
FUNCTION TO FIND MOLECULAR WEIGHT:
%Molecular weight calculation function
function W = Molecularweight(Species_sp)
% standard - atomic name
atomic_name = ['H','C','O','N','Ar'];
%Hydrogen, Carbon ,Oxygen , Nitrogen, Argon
%Standard Atomic Number
atomic_weight = [1.008 12.011 15.999 14.007 39.948];
%starting range of molecule
W = 0;
%Adding atomic number to obtain particular value
for i = 1:length(Species_sp)
for j = 1:length(atomic_name)
if strcmp(Species_sp(i),atomic_name(j))
W = W + atomic_weight(j);
position = j;
end
end
n = str2double(Species_sp);
if n>1
W = W + atomic_weight(position)*(n-1);
end
end
fprintf('molecular weight of %s :',Species_sp);
fprintf('%f',W);
disp(' ');
end
MAIN CODE:
clear all
close all
clc
f1 = fopen('THERMO.dat','r');
gt = fgetl(f1);
global_temp = fgetl(f1);
%Obtaining temperatures from header file
t = strsplit(global_temp,' ');
%Converting string cells to numbers using str2double
global_low_temp = str2double(t{2});
global_med_temp = str2double(t{3});
global_high_temp = str2double(t{4});
%universal gas constant
R =8.314;
%skipping the first 3 lines as they are comment lines
for i = 1:3
fgetl(f1);
end
for i = 1:53
line_1 = fgetl(f1);
A = strsplit(line_1,' ');
Species_sp =char((A{1}));
temp_l =str2double((A{end-3}));
temp_m =str2double((A{end-2}));
temp_h =str2double((A{end-1}));
%temperature range
temp = linspace(temp_l,temp_h,1000);
line_2 = fgetl(f1);
a= strfind(line_2,'E');
%Extracting first 7 coefficients ie. high temperature coefficients
a1 = line_2(1:a(1)+3);
a1 = str2double(a1);
a2 = line_2(a(1)+4:a(2)+3);
a2 = str2double(a2);
a3 = line_2(a(2)+4:a(3)+3);
a3 = str2double(a3);
a4 = line_2(a(3)+4:a(4)+3);
a4 = str2double(a4);
a5 = line_2(a(4)+4:a(5)+3);
a5 = str2double(a5);
line_3 = fgetl(f1);
a= strfind(line_3,'E');
%Extracting first 7 coefficients ie. high temperature coefficients
a6 = line_3(1:a(1)+3);
a6 = str2double(a6);
a7 = line_3(a(1)+4:a(2)+3);
a7 = str2double(a7);
a8 = line_3(a(2)+4:a(3)+3);
a8 = str2double(a8);
a9 = line_3(a(3)+4:a(4)+3);
a9 = str2double(a9);
a10 = line_3(a(4)+4:a(5)+3);
a10= str2double(a10);
line_4 = fgetl(f1);
a= strfind(line_4,'E');
%Extracting first 7 coefficients ie. high temperature coefficients
a11 = line_4(1:a(1)+3);
a11= str2double(a11);
a12= line_4(a(1)+4:a(2)+3);
a12= str2double(a12);
a13= line_4(a(2)+4:a(3)+3);
a13= str2double(a13);
a14= line_4(a(3)+4:a(4)+3);
a14= str2double(a14);
% %specific heat calculation
Cp= specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,temp,R,temp_m);
%enthalpy calculation
H = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,temp,R,temp_m);
%entropy calculation
S = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,temp,R,temp_m);
%calculating molecular weight of species
W = Molecularweight(Species_sp);
%plotting (cp,H,S,temp,Species_sp)
%saving as a folder
Q=sprintf('%s',Species_sp);
mkdir(Q);
cd(Q);
% %Plot 1, Temperature(cp) Vs Temperature range (temp)
%
plot(Cp,temp,'linewidth',2,'color','r');
xlabel('Temperature [k]');
ylabel('specific heat in [kJ)]');
title(sprintf('varying specific heat with temperature Range of %s',Species_sp));
grid on
saveas(figure(1),'specific heat.jpg');
%Plot 1, Enthalpy(S) Vs Temperature range (temp)
plot( H,Cp,'linewidth',2,'color','b');
xlabel('Temperature [k]');
ylabel('Enthalpy in [kJ/mol)]');
title(sprintf('varying enthalpy with temperature Range of %s',Species_sp));
grid on
saveas(figure(1),'specific heat.jpg');
%Plot 1, Entropy(S) Vs Temperature range (temp)
plot(S,temp,'linewidth',2,'color','g');
xlabel('Temperature [k]');
ylabel('Entropy in [kJ/mol-K)]');
title(sprintf('varying entropy with temperature Range of %s',Species_sp));
grid on
saveas(figure(1),'enthalpy.jpg');
cd ..
%Molecular Weight
W = Molecularweight(Species_sp);
end
STEPS :
- Firstly,we will input the parameter R and we will open the given THERMO.dat filr and eading this file using fopen command
- then, we will read the first THERMO using fgetl command and then we read temperature file and we will split the temperature using strsplit.
- Then we will assign temperature range using Linspace command . then we will run the loop to skip command lines the data using fgetl command inside the loop.
- Now, we will run the loop to finf the 53 elements in the data by assigning fget command and then splitting into the cell array to find the species line and then find the species name and then find the number of the temperatures in the species line.
- then we will read the new line. In order to read the coefficient in the data file we will first find the 'E' string using strfind command to find before and after numbers of the coefficients then we will find the remaining 14 coefficients and convert them to numbers from strings.
- Out of which we will find first 7 Coefficients which are for Higher temperature and then Second 7 Coefficients which are for lower temperature.
- Now we will specify Specific heatCp, Enthalpy H, Entropy S functions to find these parameters using these 14 coefficients for lower and higher temperature. Then we will specify the Molecular weight W function to find the molecular weight of the species .
- Now we will plot specific heat, entropy and enthalpy with the temperature for different species .
- Then we will use the saveas command to save the plots of different species i the directory folder. Now we will change the directory folder. Now we will change the directory to which our main code files runs in the MATLAB.
THE FOLDERS OF ALL THE SPECIES

THE CALCULATED MOLECULAR WEIGHT OF DIFFERENT SPECIES:
molecular weight of O :15.999000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of O :15.999000
molecular weight of O2 :15.999000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of O2 :15.999000
molecular weight of H :1.008000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of H :1.008000
molecular weight of H2 :1.008000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of H2 :1.008000
molecular weight of OH :17.007000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of OH :17.007000
molecular weight of H2O :17.007000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of H2O :17.007000
molecular weight of HO2 :17.007000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of HO2 :17.007000
molecular weight of H2O2 :17.007000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of H2O2 :17.007000
molecular weight of C :12.011000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of C :12.011000
molecular weight of CH :13.019000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of CH :13.019000
molecular weight of CH2 :13.019000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of CH2 :13.019000
molecular weight of CH2(S) :13.019000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of CH2(S) :13.019000
molecular weight of CH3 :13.019000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of CH3 :13.019000
molecular weight of CH4 :13.019000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of CH4 :13.019000
molecular weight of CO :28.010000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of CO :28.010000
molecular weight of CO2 :28.010000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of CO2 :28.010000
molecular weight of HCO :29.018000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of HCO :29.018000
molecular weight of CH2O :29.018000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of CH2O :29.018000
molecular weight of CH2OH :30.026000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of CH2OH :30.026000
molecular weight of CH3O :29.018000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of CH3O :29.018000
molecular weight of CH3OH :30.026000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of CH3OH :30.026000
molecular weight of C2H :13.019000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of C2H :13.019000
molecular weight of C2H2 :13.019000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of C2H2 :13.019000
molecular weight of C2H3 :13.019000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of C2H3 :13.019000
molecular weight of C2H4 :13.019000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of C2H4 :13.019000
molecular weight of C2H5 :13.019000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of C2H5 :13.019000
molecular weight of C2H6 :13.019000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of C2H6 :13.019000
molecular weight of CH2CO :41.029000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of CH2CO :41.029000
molecular weight of HCCO :41.029000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of HCCO :41.029000
molecular weight of HCCOH :42.037000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of HCCOH :42.037000
molecular weight of H2CN :27.026000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of H2CN :27.026000
molecular weight of HCN :27.026000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of HCN :27.026000
molecular weight of HNO :31.014000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of HNO :31.014000
molecular weight of N :14.007000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of N :14.007000
molecular weight of NNH :29.022000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of NNH :29.022000
molecular weight of N2O :30.006000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of N2O :30.006000
molecular weight of NH :15.015000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of NH :15.015000
molecular weight of NH2 :15.015000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of NH2 :15.015000
molecular weight of NH3 :15.015000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of NH3 :15.015000
molecular weight of NO :30.006000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of NO :30.006000
molecular weight of NO2 :30.006000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of NO2 :30.006000
molecular weight of HCNO :43.025000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of HCNO :43.025000
molecular weight of HOCN :43.025000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of HOCN :43.025000
molecular weight of HNCO :43.025000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of HNCO :43.025000
molecular weight of NCO :42.017000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of NCO :42.017000
molecular weight of CN :26.018000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of CN :26.018000
molecular weight of HCNN :41.033000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of HCNN :41.033000
molecular weight of N2 :14.007000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of N2 :14.007000
molecular weight of AR :39.948000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of AR :39.948000
molecular weight of C3H8 :13.019000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of C3H8 :13.019000
molecular weight of C3H7 :13.019000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of C3H7 :13.019000
molecular weight of CH3CHO :42.037000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of CH3CHO :42.037000
molecular weight of CH2CHO :42.037000
Warning: Directory already exists.
> In project1 (line 116)
molecular weight of CH2CHO :42.037000
>>
FINAL RESULT :
PLOTS FOR CO2:



PLOTS FOR O2:



PLOTS FOR N2:



ERRORS FACED WILE PROGRAMMING :

Similar errors while coding functions for entropy and enthalpy was made and corrected .
Conclusion :
From the above we can conclude that all the folders are made and the plots for o2,co2 and N2 is also done and file parsing from the given data is also done for the proect .
REFRENCES :
LEAVE A COMMENT :
https://in.mathworks.com/matlabcentral/answers/155500-how-to-extract-data-from-pdf-file-in-matlab
https://in.mathworks.com/matlabcentral/fileexchange/63615-read-text-from-a-pdf-document
https://in.mathworks.com/help/textanalytics/ug/extract-text-data-from-files.html