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:
- To write a function that extracts the 14 co-efficient and calculates the enthalpy ,entropy and specific heats for all species in the data file.
- Calculating the molecular weight of each species and displaying it in command window.
- Plotting cp, enthalpy and entropy for local temperature range specific for each species.
- Save the plot as images with appropriate names and dump them in separate folders for each species with suitable name.
THEORY:
File parsing is done to extract data from the file, it should be generic as possible to fetch the data and by using this we calculate enthalpy, entropy & specific heat for different species and condition is that the first 7 coefficients are high-temperature coefficients & the second set of 7 coefficients are low-temperature coefficients. Parsing can be defined as breaking the data block into smaller blocks by following a set of orders. so that it can be more easily interpreted, managed and sent to a computer NASA came up with the polynomials that can be used to evaluate thermodynamic properties such as C, Enthalpy, and Entropy using polynomials. They have also documented the co-efficient that are required to evaluate these polynomials for 1000+ species.
The formulas to calculate specific heat, enthalpy and entropy are given below-

SPECIFIC HEAT:
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 its temperature.
Specific heat is the thermodynamic property, which states the amount of heat required for a single unit of mass of a substance to be raised by one degree of temperature. Varying ranges of specific heat values are seen for substances depending on the extent to which they absorb heat. The term heat capacity can be misleading since heat q is the term given to the addition or removal of energy.
Cp = (a1 + a2 T + a3 T^2 + a4 T^3 + a5 T^4)*R
ENTHALPY:
Enthalpy is a concept used in science and engineering when heat and work need to be calculated. When a substance changes at constant pressure, enthalpy tells how much heat and work was added or removed from the substance. 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.
H = (a1 + a2 T /2 + a3 T^2 /3 + a4 T^3 /4 + a5 T^4 /5 + a6/T)*R*T
ENTROPY:
Entropy is an extensive property of a thermodynamic system. It is closely related to the number Ω of microscopic configurations (known as microstates) that are consistent
with the macroscopic quantities that characterize the system (such as its volume, pressure and temperature). The entropy of an object is a measure of the amount of energy which is unavailable to do work. Entropy is also a measure of the number of possible arrangements the atoms in a system can have. In this sense, entropy is a measure of uncertainty or randomness.
S = (a1 lnT + a2 T + a3 T^2 /2 + a4 T^3 /3 + a5 T^4 /4 + a7)*R
MOLECULAR WEIGHT:
The molecular mass is the mass of a given molecule: it is measured in daltons. Different molecules of the same compound may have different molecular masses because they contain different isotopes of an element.
MATLAB CODE:
FUCTIONS:
Specific heat:
function Cp = Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,global_midtemp)
%calculating specific_heat for higher and lower temperature respectively
if(T>= global_midtemp)
Cp=R*(a1+(a2*T)+(a3*(T).^2)+(a4*(T).^3)+(a5*(T).^4));
else
Cp=R*(a8+(a9*T)+(a10*(T).^2)+(a11*(T).^3)+(a12*(T).^4));
end
end
Enthalpy:
function H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,global_midtemp)
%calculating enthalpy for higher and lower temperature respectively
if(T>=global_midtemp)
H=R*((a1*T)+((a2*(T).^2)/2)+((a3*(T).^3)/3)+((a4*(T).^4)/4)+((a5*(T).^5)/5)+a6);
else
H=R*((a8*T)+((a9*(T).^2)/2)+((a10*(T).^3)/3)+((a11*(T).^4)/4)+((a12*(T).^5)/5)+a13);
end
end
Entropy:
function S=Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,global_midtemp)
%calculating entropy for higher and lower temperature respectively
if(T>= global_midtemp)
S=R*(a1.*log(T)+(a2*T)+((a3*(T).^2)/2)+((a4*(T).^3)/3)+((a5*(T).^4)/5)+a7);
else
S=R*(a8.*log(T)+(a9*T)+((a10*(T).^2)/2)+((a11*(T).^3)/3)+((a12*(T).^4)/4));
end
end
Molecular weight:
function Molecular_weight=wt(species_name)
Elements=['H' 'C' 'O' 'N' 'A' 'S'];
Atomic_weight =[1 12 16 14 40 32];
species=upper(species_name);
Molecular_weight=0;
for i=1:length(species)
for j=1:length(Elements)
if strcmp(species(i),Elements(j))
Molecular_weight=Molecular_weight+Atomic_weight(j);
k=j;
else
n=str2double(species(i));
if (n>1)
Molecular_weight=Molecular_weight+Atomic_weight(k)*(n-1);
break
end
end
end
end
end
Main program:
clear all
close all
clc
%R=molar gas constant
R=8.314;
%syntax to open the file THERMO.dat
f1=fopen('THERMO.dat','r')
%To read lines
l1=fgetl(f1);
l2=fgetl(f1);
value=strsplit(l2,' ');
%To get global temperatures from file in number form
global_lowtemp=str2num(value{2})
global_midtemp=str2num(value{3})
global_hightemp=str2num(value{4})
%temperature range
T=linspace(global_lowtemp,global_hightemp,1000);
%skipping these 3 lines
l3=fgetl(f1);
l4=fgetl(f1);
l5=fgetl(f1);
%Reading species ,calcuating cp,h,s and plotting
for i=1:53
tline=fgetl(f1);
A=strsplit(tline,' ');
name_of_species=(A{1});
lowtemp= str2num(A{end-3});
midtemp=str2num(A{end-2});
hightemp=str2num(A{end-1});
%Finding 7 higher coefficients and 7 lower coefficients
tline1=fgetl(f1);
a=findstr(tline1,'E');
a1=tline1(1:a(1)+3);
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);
a=findstr(tline2,'E');
a6=tline2(1:a(1)+3);
a6=str2num(a6);
a7=tline2(a(1)+4:a(2)+3);
a7=str2num(a7);
a8=tline2(a(2)+4:a(3)+3);
a8=str2num(a8);
a9=tline2(a(3)+4:a(4)+3);
a9=str2num(a9);
a10=tline2(a(4)+4:a(5)+3);
a10=str2num(a10);
tline3=fgetl(f1);
a=findstr(tline3,'E');
a11=tline3(1:a(1)+3);
a11=str2num(a11);
a12=tline3(a(1)+4:a(2)+3);
a12=str2num(a12);
a13=tline3(a(2)+4:a(3)+3);
a13=str2num(a13);
a14=tline3(a(3)+4:a(4)+3);
a14=str2num(a14);
%calculate specific heat(cp),Enthalpy(H),Entropy(S)
Cp=Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,global_midtemp);
H=Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,global_midtemp);
S=Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,global_midtemp);
%calculating molecular weight and printing it.
Molecular_weight=wt(name_of_species);
fprintf('molecular weight of %s= %f\n',name_of_species,Molecular_weight);
%PLOTTING CP,H,T
cd('C:\Matlab files\Project NASA')
%creating the folder
mkdir(name_of_species);
cd(name_of_species);
%plotting T vs cp
figure(1)
plot(T,Cp,'linewidth',5,'color','r')
xlabel('Temperature(T)')
ylabel('Specific heat(cp)')
grid on
title(name_of_species)
saveas(figure(1),'specific heat.jpg');
%Plotting T vs H
figure(2)
plot(T,H,'linewidth',5,'color','b')
xlabel('Temperature(T)')
ylabel('Enthalpy(H)')
grid on
title(name_of_species)
saveas(figure(2),'Enthalpy.jpg');
%Plotting T vs S
figure(3)
plot(T,S,'linewidth',5,'color','y')
xlabel('Temperature(T)')
ylabel('Entropy(S)')
grid on
title(name_of_species)
saveas(figure(3),'Entropy.jpg');
cd('C:\Matlab files\Project NASA')
end
EXPLANATION:
- Made function program.
- 'clear all' - clear all the variables in the workspace, 'close all' - closes all the plots and Figures and 'clc' - clear the command window.
- Initially the value of ‘R’ is being defined.
- Then we use a command to open the file ‘THERMO.dat’ .
- Further we use the ‘fgetl’ to start reading the lines and after reading the first two lines, from the second line converting the strings to numbers and assigning global (low,mid and high) temperatures.
- Using ‘for & end’ loops to skip the next three comment lines in the text file.
- Reading all the 53 species to plot specific heat(cp), enthalpy(H), and entropy(S), with temperature values, and to find the molecular weights.
- Calling specific heat, enthalpy, entropy function codes.
- Calculating Molecular weight using functions.
- Plotting the graph.
RESULTS:
- Plots for oxygen(o2) :



- Plots for nitrogen(N2) :



Plots for carbon dioxide(co2) :



Molecular weight:



MATLAB FOLDER SCREENSHOT:

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.