Project 1 - Parsing NASA thermodynamic data
Aim :-
write a code for parsing NASA thermodynamic data file to calculate thermodynamic properties of various species.
Objective :-
- Write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.
- Calculate the molecular weight of each species.
- Plot the Cp, Enthalpy and Entropy for the local temperature range for each species.
- Write a detailed report to explain the logic of code.
Theory :-
What is data 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, confirming to the rules of a formal grammar.
Parsing in computer languages refers to syntactic analysis of the input code into its components parts in order to facilitate the writing of compilers and interpreters. It can be used to describe split or separation.
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.
Mathematical equations used :-

Where,
R = universal gas constant
T = Temperature (k)
Cp = Specific heat (KJ)
S = Entropy (KJ/mol-k)
H = Enthalpy(kj/mol)
- Specific heat :-
Specific heat is defined as the amount of heat required to raise to the temperature of one gram of a substance by one Celsius degree.
- Enthalpy :-
Enthalpy is the total heat present in the system
- Entropy :-
Entropy is defined as the measure of a systems thermal energy per unit temperature that is unavailable for doing useful work.
Main code :-
It also includes the conditions for the code to find Specific heat, Enthalpy, Entropy.
% Parsing nasa thermodyanamic file
clear all
close all
clc
%Open and read the file
f1 = fopen('THERMO.dat','r')
for i = 1:5
fgetl(f1);
end
%Finding local temperatures from line1
for j=1:53
tline1 = fgetl(f1);
splitting_tline1 = strsplit(tline1,' ');
species_name = char(splitting_tline1(1));
find_dot = strfind(tline1,'.');
local_low_temp = str2double(tline1(find_dot(1)-3:find_dot(1)+3));
local_medium_temp = str2double(tline1(find_dot(2)-4:find_dot(2)+3));
local_high_temp = str2double(tline1(find_dot(3)-4:find_dot(3)+3));
%Extracting coefficients from line2
tline2 = fgetl(f1);
find_e = strfind(tline2,'E');
a1 = str2double(tline2(1:find_e(1)+3));
a2 = str2double(tline2(find_e(1)+4:find_e(2)+3));
a3 = str2double(tline2(find_e(2)+4:find_e(3)+3));
a4 = str2double(tline2(find_e(3)+4:find_e(4)+3));
a5 = str2double(tline2(find_e(4)+4:find_e(5)+3));
%Extracting coefficients from line3
tline3 = fgetl(f1);
find_e = strfind(tline3,'E');
a6 = str2double(tline3(1:find_e(1)+3));
a7 = str2double(tline3(find_e(1)+4:find_e(2)+3));
a8 = str2double(tline3(find_e(2)+4:find_e(3)+3));
a9 = str2double(tline3(find_e(3)+4:find_e(4)+3));
a10 = str2double(tline3(find_e(4)+4:find_e(5)+3));
%Extracting coefficients from line4
tline4 = fgetl(f1);
find_e = strfind(tline4,'E');
a11 = str2double(tline4(1:find_e(1)+3));
a12 = str2double(tline4(find_e(1)+4:find_e(2)+3));
a13 = str2double(tline4(find_e(2)+4:find_e(3)+3));
a14 = str2double(tline4(find_e(3)+4:find_e(4)+3));
%Defining temp. range and 'R'
T=(local_low_temp:local_high_temp);
R=287;
%Finding values for 'H', 'S', 'Cp'
if (T>local_medium_temp);
%specific heat
Cp = R*(a1 + (a2*T) + (a3*(T.^2)) + (a4*(T.^3)) + (a5*(T.^4)));
%Enthaply
H = R*(a1 + (a2*T/2) + (a3*(T.^2)/3) + (a4*(T.^3)/4) + (a5*(T.^4)/5) + (a6*(T)));
%Entropy
S = R*((a1*log(T)) + (a2*T) + (a3*(T.^2)/2) + (a4*(T.^3)/3) + (a5*(T.^4)/4) + a7);
else
%specific heat
Cp = R*(a8 + (a9*T) + (a10*(T.^2)) + (a11*(T.^3)) + (a12*(T.^4)));
%Enthaply
H = R*(a8 + (a9*T/2) + (a10*(T.^2)/3) + (a11*(T.^3)/4) + (a12*(T.^4)/5) + (a13*(T)));
%Entropy
S = R*((a8*log(T)) + (a9*T) + (a10*(T.^2)/2) + (a11*(T.^3)/3) + (a12*(T.^4)/4) + a14);
end
% Calling function to calculate molecular weight of species
molecular_weight = m_wt(species_name)
sprintf('molecular weight of %s=%d',species_name,molecular_weight)
Q=sprintf('%s',species_name)
mkdir(Q)
cd(Q)
%Plotting the curves
%Cp v temperature plot
figure(1)
plot(Cp,T, 'b');
xlabel('specific heat')
ylabel('Temperature')
Cp_text = sprintf('Specific heat v temperature for %s', species_name);
title(Cp_text);
saveas(1, 'Cp.jpg');
%Enthalpy v temperature plot
figure(2)
plot(H, T, 'r');
xlabel('Enthalpy')
ylabel('Temperature')
H_text = sprintf('Enthalpy v temperature for %s', species_name);
title(H_text);
saveas(2, 'H.jpg');
%Entropy v temperature plot
figure(3)
plot(S, T, 'g');
xlabel('Entropy')
ylabel('Temperature')
S_text = sprintf('Entropy v temperature for %s', species_name);
title(S_text);
saveas(3, 'S.png');
cd ..
end
Code explanation :-
- Open and read file using ‘fopen’, and ‘fgetl’, command.
- Using forloop to find values for all 53 species.
- Reading first line and splitting line using ‘strsplit’ command by taking reference of space
- Then reading local temperatures using strsplit by taking reference of dot.
- Extracting all 14 coefficients and conver it into number using ‘str2double’ command.
- Defining value for universal gas constant and defining temp range.
- Applying conditions to calculate ‘Cp’, ‘H’, ‘S’.
- Calling function to find values of molecular weight.
- Making directory to save all the plots in same folder.
- Making plots of ‘Cp’, ‘H’, ‘S’ for all 53 species.
- Making labels and titles for plots.
Function to find molecular weight : -
% Function for calculating molecular weight
function [molecular_weight] = m_wt(spec)
% Defining the elements
elements = ['H','C','N','O','R'];
% Atomic weight of each element
Atomic_weight = [1 12 14 16 40];
molecular_weight = 0;
% Looping for each element inside each species and finding the automic
% weight
for i = 1:length(spec)
for j = 1:length(elements)
if strcmp(spec(i),elements(j))
molecular_weight = molecular_weight + Atomic_weight(j);
end
end
end
end
Code explanation :-
- Making function to calculate .
- Defining base elements present in each species.
- Defining atomic weight of each element.
- Making forloop to calculate molecular weight of all species
- Comparing species with elemts using ‘strcmp’ command.
- Writing formula for molecular weight.
Plots for O2 :-



Plots for N2 :-



Plots for CO2 :-



Screenshot of window where all the plots of species are saved :-

Output for molecular weight :-




Conclusion :-
From this assignment we learn the use of forloop to calculate thermodynamic properties of different species. We learn how to parse the data from the file and use it to find values.