Project 1 - Parsing NASA thermodynamic data
File parsing:
Parsing, syntax analysis, or syntactic analysis is the process of analyzing a string of symbols, either in natural language, computer languages, or data structures, conforming to the rules of a formal grammar. The term parsing comes from Latin pars, meaning part.
File parsing in computer language means to give a meaning to the characters of a text file as per the formal grammar.
NASA Thermodynamic File:
NASA thermodynamic file contains 14 coefficients for 53 elements. These coefficients are utilized to calculate the specific heat, enthalpy, and entropy for a particular element. In the 14 coefficients, the first 7 coefficients are high-temperature coefficients (above 1000K), and the last 7 coefficients are low-temperature coefficients (below 1000K).
The given equations are:
- Specific Heat(cp):- The specific heat of a substance is the heat capacity of a sample of the substance divided by the mass of the sample. Informally, it is the amount of energy that must be added, in the form of heat, to one unit of mass of the substance in order to cause an increase of one unit in temperature.
- Enthalpy:- It is a property of a thermodynamic system, defined as the sum of the system's internal energy and the product of its pressure and volume.
- Entropy:- It is the measure of a system's thermal energy per unit temperature that is unavailable for doing useful work. Because work is obtained from ordered molecular motion, the amount of entropy is also a measure of the molecular disorder, or randomness, of a system.

Where,
R = universal gas constant,
T = Temperature,
Cp = specific heat (KJ)
s = Entropy (KJ/mol-K)
H = Enthalpy (KJ/mol)
a(i) = Numerical coefficients [where a1, a2, a3, a4, a5, a6, and a7 are high temp coefficients and a8, a9, a10, a11, a12, a13, and a14 are low temp coefficients]
FUNCTION CODE:-
Specific heat:
function [Cp] = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,mid_temp,R,t)
if (t < mid_temp)
Cp = R*(a8+(a9.*t)+(a10.*(t.^2))+(a11.*(t.^3))+(a12.*(t.^4)));
else
Cp = R*(a1+(a2.*t)+(a3.*(t.^2))+(a4.*(t.^3))+(a5.*(t.^4)));
end
end
Enthalpy:
function [H] = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,mid_temp,R,t)
if (t < mid_temp)
H = (a8+(a9.*t./2)+(a10.*(t.^2)/3)+(a11.*(t.^3)/4)+(a12.*(t.^4)/5)+(a13./t)).*t*R;
else
H = (a1+(a2.*t./2)+(a3.*(t.^2)/3)+(a4.*(t.^3)/4)+(a5.*(t.^4)/5)+(a6./t)).*t*R;
end
end
Entropy:
function [S] = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,mid_temp,R,t)
if (t < mid_temp)
S = R*(a8*log(t)+(a9.*t)+(a10.*(t.^2)/2)+(a11.*(t.^3)/3)+(a12.*(t.^4)/4)+(a14));
else
S = R*(a1*log(t)+(a2.*t)+(a3.*(t.^2)/2)+(a4.*(t.^3)/3)+(a5.*(t.^4)/4)+(a7));
end
end
Specific weight:-
function [W] = molecular_weight(species)
% Atomic name
atomic_name = ['H','O','N','C','Ar'];
% Atomic mass
atomic_mass = [1.008 15.999 14.077 12.011 39.948];
W = 0;
for i = 1:length(species)
for j = 1:length(atomic_name)
if strcmp(species(i),atomic_name(j));
W = W + atomic_mass(j);
P = j;
end
end
n = str2num(species);
if n>1
W = W + atomic_mass(P)*(n-1);
end
end
fprintf('molecular weight of %s',species);
fprintf('%f',W);
disp(' ')
end
Main Code:-
clear all
close all
clc
% Input
R = 8.314; % gas constant
% reading data file
f1 = fopen('THERMO.dat','r');
A = fgetl(f1)
% temperatures
temp = fgetl(f1);
% split
s = strsplit(temp);
% converting strings to numbers
global_low_temp = str2num(s{2});
global_mid_temp = str2num(s{3});
global_high_temp = str2num(s{4});
% skipping
for i = 1:3
A = fgetl(f1);
end
% Reading species
for j = 1:53
tline = fgetl(f1);
A = strsplit(tline,' ');
species = (A{1});
% converting
low_temp = str2num(A{end-3});
high_temp = str2num(A{end-2});
mid_temp = str2num(A{end-1});
% temperature range
t = linspace(low_temp,high_temp,1000);
tline1 = fgetl(f1);
a = strfind(tline1,'E');
a1 = tline1(1:a(1)+3); %% a1 to a7 high temp coefficients
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);
b = strfind(tline2,'E');
a6 = tline2(1:b(1)+3);
a6 = str2num(a6);
a7 = tline2(b(1)+4:b(2)+3);
a7 = str2num(a7);
a8 = tline2(b(2)+4:b(3)+3); %% a8 to a14 low temp coefficients
a8 = str2num(a8);
a9 = tline2(b(3)+4:b(4)+3);
a9 = str2num(a9);
a10 = tline2(b(4)+4:b(5)+3);
a10 = str2num(a10);
tline3 = fgetl(f1);
c = strfind(tline3,'E');
a11 = tline3(1:c(1)+3);
a11 = str2num(a11);
a12 = tline3(c(1)+4:c(2)+3);
a12 = str2num(a12);
a13 = tline3(c(2)+4:c(3)+3);
a13 = str2num(a13);
a14 = tline3(c(3)+4:c(4)+3);
a14 = str2num(a14);
% calculating specific heat
Cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,global_mid_temp,R,t);
% calculating enthalpy
H = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,global_mid_temp,R,t);
% calculating entropy
S = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,global_mid_temp,R,t);
% specific weight
W = molecular_weight(species);
% saving the plots in folder
% currentfolder = pwd;
mkdir(species);
cd(species);
% plot Cp Vs t
figure(1)
plot(t,Cp,'linewidth',2,'color','r');
xlabel('Temperature');
ylabel('Specific-heat');
grid on;
title(sprintf('Temperature Vs Specific-heat with temp range of %s',species));
saveas(figure(1),'Specific-heat.jpg');
% plot H Vs t
figure(2)
plot(t,H,'linewidth',2,'color','b');
xlabel('Temperature');
ylabel('Enthalpy');
grid on;
title(sprintf('Temperature Vs Enthalpy with temp range of %s',species));
saveas(figure(2),'Enthalpy.jpg');
% plot S Vs t
figure(3)
plot(t,S,'linewidth',2,'color','g');
xlabel('Temperature');
ylabel('Entropy');
grid on;
title(sprintf('Temperature Vs Entropy with temp range of %s',species));
saveas(figure(3),'Entropy.jpg');
cd ..
end
Commands used in the code:-
- fgetl is used to read a line from the file, removing newline characters.
- strsplit is used to split string or character vector at specified delimiter.
- str2num is used to convert a string to a number.
- strcmp is used to comparing the strings.
- strfind is used to find strings within other strings.
- pwd is used to identify the current folder.
- mkdir is used to create or make a new folder.
The explanation for the code:-
- Open the 'THERMO.dat' file by the command 'fopen' and by the command 'fgetl' we can read the open file.
- To obtain the temperature range from the data file, use 'strlin' command to split the temperature into the string cell. After that use 'str2num' to convert the string to a number.
- After temperature skips the header part.
- There are 53 elements and 14 coefficients in the data file, so by using 'for' and 'end' we can read all those things and plot the specific heat, enthalpy and entropy to temperature.
- After that find the molecular weight of all species.
Results For O2, N2, and CO2:-









The molecular weight of the species:



Errors:-

we have to use . after t and R.

By writing line 91 the new species plots are creating the same species to avoid that error just remove the line.
Conclusion:-
By using the matlab we can calculate and plot the specific heat, enthalpy, and entropy to the temperature for the different species which are given in the nasa thermodynamic data file. We can also calculate the specific weight of the each species which are mentioned in the data file.