Project 1 - Parsing NASA thermodynamic data
AIM
Write code in matlab to parising the NASA thermodynamics data file .
OBJECTIVE
- Write a function that extracts the 14 coefficients and calculates the enthalpy,entropy,specific heat.

- Calculate molecular weight of each species and display in command window.
- Plot the Cp , Entahalpy, Entropy for the local temperature range.
- Save plots as images with approximate names make them into seperate folder for each species.
- Screenshot of the window where all folders are saved.
- Plot o2, N2,C02 species.
FILE PARSING
Parsing or Synatx analysis is the process of analyzing a string of symbols either in nature language , Computer language or data, structures conforming to the rule sof a formal Grammar. Within computational linguistics the term is used to refer to the formal analysis by a computer of a sentence into its constituents, resulting in parse tree showing their syntax relation to each other which may be also contaion sematic and other information . Some parsing allgorithims may generate a parse forest or list of parse trees for synatx ambiguos input.
- Step 1 Define the function step
- Step 2 Create a input parser object
- Step 3 A dd input to scheme
- Step 4 Set properties to adjust parsing
- Step 5 Parse inputs
- Step 6 Use inputs in function
- Step 7 Calling function
NASA Thermodynamic file
THERMA.dat file is a data file containing 14 coefficients for 53 differnt elements given by NASA .These coefficients are utilized to calculate the specific heat , enthalpy entropy , for paticular element . Each element has is own range of local temperature.
List of elements in thermo.dat file

MAIN CODE
Synatx used for code
Fopen Open file information about files
r Open the file for reading
fgetl Read line from file
strsplit Split string or character vector at specified delimeter
str2num Convert charcter array or string to numeric array
strfind Find one string within another
linspace Generate linearly space vectors
fprintf write formatte data to file
pwd Identify current directory
mkdir Make new directory
cd Change working directory
% SUBMITTED BY VIKAS
% NASA THERMODYANMICS FILE PAIRSING
clear all
close all
clc
% MAKE DIRECTORY
mkdir 'C:\Users\Vikas\Desktop\fileparsing'
% READING DATA FILE
% OPEN FILE AND READING THERMO.dat FILE
f1=fopen('THERMO.dat','r')
gt=fgetl(f1);
% READING TEMPERATURE FROM THIS FILE
global_temp=fgetl(f1);
% SPLITTING
t=strsplit(global_temp);
% CONVERTING STRINGS CELLS INTO NUMBERS,
global_low_temp=str2num(t{2});
global_medium_temp=str2num(t{3});
global_high_temp=str2num(t{4});
% TEMPERATURE RANGE
Temp=linspace(global_low_temp,global_high_temp,1000);
% INPUT GAS CONSTANT J/(mol-K)
R = 8.314;
% SKIPPING COMMENT LINES
for i = 1:3
gt=fgetl(f1);
end
for i = 1:53
gt=fgetl(f1);
A=strsplit(gt,' ');
Species_SP=(A{1})
line_1=fgetl(f1) ;
a=strfind(line_1,'E');
% FINDING HIGHER COEFFICIENTS
% FINDING FIRST 7 HIGHER AND SECOND 7 LOWER COEFFICIENTS
a1=line_1(1:a(1)+3);
a1=str2num(a1);
a2=line_1(a(1)+4:a(2)+3);
a2=str2num(a2) ;
a3=line_1(a(2)+4:a(3)+3);
a3=str2num(a3) ;
a4=line_1(a(3)+4:a(4)+3);
a4=str2num(a4);
a5=line_1(a(4)+4:a(5)+3);
a5=str2num(a5) ;
line_2=fgetl(f1);
b=strfind(line_1,'E');;
a6=line_2(1:b(1)+3);
a6=str2num(a6) ;
a7=line_2(a(1)+4:b(2)+3);
a7=str2num(a7);
% FINDING LOWER COEFFICIENTS
a8=line_2(b(2)+4:b(3)+3);
a8=str2num(a8);
a9=line_2(b(3)+4:b(4)+3);
a9=str2num(a9) ;
a10=line_2(b(4)+4:b(5)+3);
a10=str2num(a10) ;
line_3=fgetl(f1);
c=strfind(line_1,'E');
a11=line_3(1:c(1)+3);
a11=str2num(a11);
a12=line_3(c(1)+4:c(2)+3);
a12=str2num(a12);
a13=line_3(c(2)+4:c(3)+3);
a13=str2num(a13);
a14=line_3(c(3)+4:c(4)+3);
a14=str2num(a14);
% CALCULATE SPECIFIC HEAT
Cp = Specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,Temp,R,global_medium_temp);
% CALCULATE ENTHALPY
H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,Temp,R,global_medium_temp);
% CALCULATE ENTROPY
S = Entropy_calc(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,Temp,R,global_medium_temp);
% CALCULATE MOLECULAR WEIGHT SPECIES
Molecularweight(i) = Molecular_weight(Species_SP);
% WRIITNG MOLECULAR WEIGHT FILE
Molecular_list = fopen('Molecular weight.txt','a+');
fprintf( Molecular_list,'Molecular weight of %s is %d n',Species_SP,Molecularweight);
fclose(Molecular_list);
% PLOTTING(CP,H,S,Temp,Species_SP)
% SAVING IN FOLDER
cur_dir=pwd;
mkdir(Species_SP);
cd(Species_SP);
% PLOT 1 (CP) vs (Temp)
figure(1)
plot(Temp,Cp,'linewidth',3,'color','b');
xlabel('TEMPERATURE [K]');
ylabel('SPECIFIC HEAT[kJ]');
title(sprintf('Specific Heat VS Temperature Range of %s',Species_SP));
grid on
saveas(figure(1),'SPECIFIC HEAT.jpg');
% PLOT 2 (H) vs (Temp)
figure(2)
plot(Temp,H,'linewidth',3,'color','y');
xlabel('TEMPERATURE [K]');
ylabel('ENTHALPY IN [kJ/(mol)]');
title(sprintf('Enthalpy VS Temperature Range of %s',Species_SP));
grid on
saveas(figure(2),'ENTHALPY.jpg');
% PLOT 3 (S) vs (Temp)
figure(3)
plot(Temp,S,'linewidth',3,'color','g');
xlabel('TEMPEARTURE [K]');
ylabel('ENTROPY IN [kJ/(mol-K)]');
title(sprintf('Entropy VS Temperature Range of %s',Species_SP));
grid on
saveas(figure(3),'ENTROPHY.jpg');
cd(cur_dir);
% MOLECULAR WEIGHT
M = Molecular_weight(Species_SP)
f2 = fopen('molecular mass','w');
fprintf('%s n %s n %f n','species','Molecular mass',M)
fclose(f2)
end
CODE EXPLANATION
- Clear all clear all the variables in workspace.
- Close all Close all plots and figures
- clc Clears the command window.
- Making a directory using mkdir with location adress.
- Reading the text file Thermo.dat file and opening it using fopen and r as reading command
- For reading the line in a text using fgetl
- To obtain the temperature range from the file using strsplit to the split the temperature into a particular string cell.
- Used str2num for converting string to cell numbers
- Divided the tempearture into the string and then converted to a number so that temperature values can be used sepeartely to solve the NASA thermodynamics
- Using for and end loops to skip the next three comments lies in the text file
- Reading all the 53 species to plot specific heat , enthalpy , and entropy with tempertaure values to find molecular weights
- Used for loop to read 53 species
- Used strfind to find find string
- Using linspace spcified the temperature range from low to high.
- Adding universal gas constant value r
- Calling spsecific heat , enthalpy , entropy function codes.
- Calculating molecular weight.
ENTHALPY
Enthalpy a property of a thermodynamic system is equal to the system internal energy plus the product of its pressure and volume In a system contained as to prevent mass transfer for process at constant pressure the heat absorbed or released equals the change in enthalpy .
CODE
% SUBMITTED BY VIKAS
% ENTHALPY
function H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,Temp,R,global_medium_temp)
% COMPARING WITH GLOBAL TEMPERATURE
% CALCULATING ENTHALPY FOR HIGHER AND LOWER TEMPERATURE RESPECTIVELY
if Temp <= global_medium_temp
H = R*Temp.*(a8 + ((a9*Temp)/2) + ((a10*(Temp).^2)/3) + ((a11*(Temp).^3)/4) + ((a12*(Temp).^4)/5) +((a13./Temp)));
else
H = R*Temp.*(a1 + ((a2*Temp)/2) + ((a3*(Temp).^2)/3) + ((a4*(Temp).^3)/4) + ((a5*(Temp).^4)/5) +((a6./Temp)));
end
- Now we will find the enthalpy by creating enthalpy function and assigning its related formula coefficient from 14 coefficient , gas constant , temperature and mid temperature.
- Now will create an condition using if loop and make the temperature less than and equal to mid temperature to specify lower temperature coefficient formula
- Then will use else condition to specify coefficient of higher temperature coefficient in the formula
- Then end the loop and function to calculate the enthalpy
ENTROPY
Entropy the measure of the system thermal energy per unit temperature that is unavailable for useful work . Because Work is obtained from ordered molecular motion the amount of entropy is also measure of the molecular disorder or randomness of a system . Them concept of provides deepinsight into the direction of spontaneous change for many everyday phenomena

CODE
% SUBMITTED BY VIKAS
% ENTROPY
function S = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,Temp,R,global_medium_temp)
% COMPARING WITH GLOBAL TEMPERATURE
% CALCULATING ENTROPY FOR HIGHER AND LOWER TEMPERATURE RESPECTIVELY
if Temp <= global_medium_temp
S = R.*(a8.*log(Temp) + (a9.*Temp) + (a10*((Temp).^2)/2) + (a11*((Temp).^3)/3) + (a12*((Temp).^4)/4) + a14);
else
S = R.*(a1.*log(Temp) + (a2.*Temp) + (a3*((Temp).^2)/2) + (a4*((Temp).^3)/3) + (a5*((Temp).^4)/4) + a7);
end
- Now we will find the entropy by creating entropy function and assigning its related formula coefficient from 14 coefficient , gas constant , temperature and mid temperature.
- Now will create an condition using if loop and make the temperature less than and equal to mid temperature to specify lower temperature coefficient formula
- Then will use else condition to specify coefficient of higher temperature coefficient in the formula
- Then end the loop and function to calculate the entropy
SPECIFIC HEAT
It is amount of energy that must be added in the form of heat to one unit of mass of the subsatance in order to cause an increase of one unit in its temperature.

CODE
% SUBMITTED BY VIKAS
% SPECIFIC HEAT
function Cp = Specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,Temp,R,global_medium_temp)
% COMPARING WITH GLOBAL TEMPERATURE
% CALCULATING SPECIFIC HEAT FOR HIGHER AND LOWER TEMPERATURE RESPECTIVELY
if Temp <= global_medium_temp
Cp = R*(a8 + (a9*Temp) + (a10*(Temp).^2) + (a11*(Temp).^3) + (a12*(Temp).^4));
else
Cp = R*(a1 + (a2*Temp) + (a3*(Temp).^2) + (a4*(Temp).^3) + (a5*(Temp).^4));
end
- Now we will find the Specific heat by creating Specific heat function and assigning its related formula coefficient from 14 coefficient , gas constant , temperature and mid temperature.
- Now will create an condition using if loop and make the temperature less than and equal to mid temperature to specify lower temperature coefficient formula
- Then will use else condition to specify coefficient of higher temperature coefficient in the formula
- Then end the loop and function to calculate the Specific heat
MOLECULAR WEIGHT FOR DIFFERENT SPECIES
The molecular mass is the mass of given molecule It is measured in daltons Different molecules of the same compound may be have different molecular masses because they contain different istotopes of an element.
CODE
% SUBMITTED BY VIKAS
% MOLECULAR WIEGHT
function Molecularweight = Molecular_weight(Species_SP)
% DEFINING NAME OF SPECIES
atomic_name = ['H','C','O','N','A'];
% HYDROGEN, CARBON, OXYGEN, NITROGEN, ARGON
fprintf('Molecular Weight of ');
fprintf(Species_SP);
fprintf(' : ');
% STANDARD ATOMIC NUMBER
Atomic_Weight = [1.008 12.011 15.999 14.007 39.948];
% STARTING RANGE OF MOLECULE
Molecularweight = 0;
for i = 1:length(Species_SP)
for j = 1:length(atomic_name)
if strcmpi(Species_SP(i),atomic_name(j))
Molecularweight = Molecularweight +Atomic_Weight(j);
position = j;
end
end
n = str2num(Species_SP(i));
if n>1
Molecularweight = Molecularweight + Atomic_Weight(position)*(n-1);
end
end
fprintf('molecular weight %s :',Species_SP);
fprintf('%d',Molecularweight);
disp(' ');
end
- Firstly we create an Molecular_weight function w and assigning species input to that
- Then define the main element of all species in an array that are H C O N Ar
- Then by taking above elements we will create there atomic weight from the source of internet that 1.008 12.011 15.999 114.007 39.948
- Then we will create an condition using if condition and use strcmp command to comapare the species and atomic name strings to calculate molecular masses if by comparsion both string found same then we will assign its molecular weight
- Then we will print the molecular weight usoing fprintf command and display it using disp command
RESULT AND OUTPUT


FILE LOCATION


O2 SPECIES RESULT

N2 SPECIES RESULT


CO 2 SPECIES RESULT


MOLECULAR OUTPUTS
Molecular Weight of O : molecular weight O :1.599900e+01
Molecular Weight of O : molecular weight O :1.599900e+01
M =
15.9990
species n Molecular mass n 15.999000 n
ans =
0
Species_SP =
'O2'
Molecular Weight of O2 : molecular weight O2 :3.199800e+01
Molecular Weight of O2 : molecular weight O2 :3.199800e+01
M =
31.9980
species n Molecular mass n 31.998000 n
ans =
0
Species_SP =
'H'
Molecular Weight of H : molecular weight H :1.008000e+00
Molecular Weight of H : molecular weight H :1.008000e+00
M =
1.0080
species n Molecular mass n 1.008000 n
ans =
0
Species_SP =
'H2'
Molecular Weight of H2 : molecular weight H2 :2.016000e+00
Molecular Weight of H2 : molecular weight H2 :2.016000e+00
M =
2.0160
species n Molecular mass n 2.016000 n
ans =
0
Species_SP =
'OH'
Molecular Weight of OH : molecular weight OH :1.700700e+01
Molecular Weight of OH : molecular weight OH :1.700700e+01
M =
17.0070
species n Molecular mass n 17.007000 n
ans =
0
Species_SP =
'H2O'
Molecular Weight of H2O : molecular weight H2O :1.801500e+01
Molecular Weight of H2O : molecular weight H2O :1.801500e+01
M =
18.0150
species n Molecular mass n 18.015000 n
ans =
0
Species_SP =
'HO2'
Molecular Weight of HO2 : molecular weight HO2 :3.300600e+01
Molecular Weight of HO2 : molecular weight HO2 :3.300600e+01
M =
33.0060
species n Molecular mass n 33.006000 n
ans =
0
Species_SP =
'H2O2'
Molecular Weight of H2O2 : molecular weight H2O2 :3.401400e+01
Molecular Weight of H2O2 : molecular weight H2O2 :3.401400e+01
M =
34.0140
species n Molecular mass n 34.014000 n
ans =
0
Species_SP =
'C'
Molecular Weight of C : molecular weight C :1.201100e+01
Molecular Weight of C : molecular weight C :1.201100e+01
M =
12.0110
species n Molecular mass n 12.011000 n
ans =
0
Species_SP =
'CH'
Molecular Weight of CH : molecular weight CH :1.301900e+01
Molecular Weight of CH : molecular weight CH :1.301900e+01
M =
13.0190
species n Molecular mass n 13.019000 n
ans =
0
Species_SP =
'CH2'
Molecular Weight of CH2 : molecular weight CH2 :1.402700e+01
Molecular Weight of CH2 : molecular weight CH2 :1.402700e+01
M =
14.0270
species n Molecular mass n 14.027000 n
ans =
0
Species_SP =
'CH2(S)'
Molecular Weight of CH2(S) : molecular weight CH2(S) :1.402700e+01
Molecular Weight of CH2(S) : molecular weight CH2(S) :1.402700e+01
M =
14.0270
species n Molecular mass n 14.027000 n
ans =
0
Species_SP =
'CH3'
Molecular Weight of CH3 : molecular weight CH3 :1.503500e+01
Molecular Weight of CH3 : molecular weight CH3 :1.503500e+01
M =
15.0350
species n Molecular mass n 15.035000 n
ans =
0
Species_SP =
'CH4'
Molecular Weight of CH4 : molecular weight CH4 :1.604300e+01
Molecular Weight of CH4 : molecular weight CH4 :1.604300e+01
M =
16.0430
species n Molecular mass n 16.043000 n
ans =
0
Species_SP =
'CO'
Molecular Weight of CO : molecular weight CO :2.801000e+01
Molecular Weight of CO : molecular weight CO :2.801000e+01
M =
28.0100
species n Molecular mass n 28.010000 n
ans =
0
Species_SP =
'CO2'
Molecular Weight of CO2 : molecular weight CO2 :4.400900e+01
ERRORS


CONCLUSION
Using matlab code the necessary coefficients required to evaluate various thermodynamic parameters for different species extracted from NASA thermodynamic data file . The specific heat , enthalpy , entropy for all the species sre calculated and plotted aganist the local temperature Range.
LINK
https://drive.google.com/drive/folders/1BEzQnB0kSNE-JT-aJOdYSkn5TNTkzbaS?usp=sharing
REFERNECES