Project 1 - Parsing NASA thermodynamic data
AIM :
- Write code in MATLAB to parse the NASA thermodynamic data file.
- write a function that extracts the 14 co-efficient the enthalpy,entropy, and specific heat for all the species in the data file.
- Calculating the molecular weight of each species and displaying it in the command window.
- plotting the cp, Enthalpy, and Entropy for local temp. range (low temp. : high temp) specific for each species.
- Saving the plots as images with appropriate names and dumping them in separate folders for each species with suitable name.
THEORY :
- SO, NASA came up with polynomials that can be used to evaluate thermodynamics properties such as Cp, H and S.
- They have also documented the co-efficients that are required to evaluate these polynomials for 1000+ species.
- The thermodynamic properties in terms of the co-efficients are given as:

- The First 7 Co-efficients are high-temperature co-efficients
- The Second 7 Co-efficients are low-temperature co-efficients
- R is Universal gas constant & T is the temperature.
SPECIFIC HEAT : It is define as the amount of heat required to raise the temperature of unit mass of a substance by 1 degree.
ENTHALPY : It is sum of the internal energy and the product of the pressure and volume of a thermodynamic process.Enthalpy is an energy like property, it has the dimensions of energy and its value is determined entirely by the temperature, pressure and composition of the system
ENTROPY : It is the measure of a system's themal energy per unit temperature that is unavailable for doing useful work beacuse work is obtained from ordered moleculer motion, the amount of entropy is also a measure of the moleculer disorder, or randomness of a system.
STEP-WISE EXPLATION OF PROGRAM :
- First Start with Clear all,Close all,clc Commands,
- we open the thermodynamics data using fopen command and use fgetl command to read the contex of a file,
- Then obtaining the values of globel temperaturesThen we will extract the individual species data of all the species present in the file using fgetl command to read the file line by line and skipping the line not containing useful data,
- The data that is to be extracted for all the 53 species mentioned in the file are 14 co-efficient(7 for higher temperature & 7 for lower temperature) and we will use For Loop commad for this process,
- Then we will use FUNCTION command and calculate Specific Heat, Enthalpy ,Entropy and Moleculer Weight in Separate Script,
- Then We will plot specific heat v/s temperature , enthalpy v/s temperature and entropy v/s temperature for all the 53 species and these 3 plots will be saved in separatee folders for each species.
MATLAB MAIN PROGRAM :
close all
clear all
clc
%opening the external file
f1 = fopen('THERMO.dat','r')
line_1 = fgetl(f1);
line_2 = fgetl(f1);
%obtaining global temperature values
global_values = strsplit(line_2,' ');
g_low_temp = str2num(global_values{2});
g_mid_temp = str2num(global_values{3});
g_high_temp = str2num(global_values{4});
line_3 = fgetl(f1);
line_4 = fgetl(f1);
line_5 = fgetl(f1);
%defining input universal gas constant
R = 8.314;
for i=1:53
T=fgetl(f1);
%extracting the spacies name
spacies_name = T(1:9);
spacies_name = deblank(spacies_name);
%extracting local temperature
spacies_temp = T(49:75);
local_values = strsplit(spacies_temp,' ');
l_low_temp = str2num(local_values{1});
l_mid_temp = str2num(local_values{2});
l_high_temp = str2num(local_values{3});
%storing local temperature in a variable
temp = [l_low_temp l_mid_temp l_high_temp];
temp_range = linspace(l_low_temp,l_high_temp,500);
coeff = fgetl(f1);
coeff1 = findstr(coeff,'E');
%extracting the coefficients
a1 = coeff(1:coeff1(1)+3);
a1 = str2num(a1);
a2 = coeff(coeff1(1)+4:coeff1(2)+3);
a2 = str2num(a2);
a3 = coeff(coeff1(2)+4:coeff1(3)+3);
a3 = str2num(a3);
a4 = coeff(coeff1(3)+4:coeff1(4)+3);
a4 = str2num(a4);
a5 = coeff(coeff1(4)+4:coeff1(5)+3);
a5 = str2num(a5);
new_line = fgetl(f1);
coeff2 = findstr(new_line,'E');
a6 = new_line(1:coeff2(1)+3);
a6 = str2num(a6);
a7 = new_line(coeff2(1)+4:coeff2(2)+3);
a7 = str2num(a7);
a8 = new_line(coeff2(2)+4:coeff2(3)+3);
a8 = str2num(a8);
a9 = new_line(coeff2(3)+4:coeff2(4)+3);
a9 = str2num(a9);
a10 = new_line(coeff2(4)+4:coeff2(5)+3);
a10 = str2num(a10);
new_line1 = fgetl(f1);
coeff3 = findstr(new_line1,'E');
a11 = new_line1(1:coeff3(1)+3);
a11 = str2num(a11);
a12 = new_line1(coeff3(1)+4:coeff3(2)+3);
a12 = str2num(a12);
a13 = new_line1(coeff3(2)+4:coeff3(3)+3);
a13 = str2num(a13);
a14 = new_line1(coeff3(3)+4:coeff3(4)+3);
a14 = str2num(a14);
%Specific Heat Calculation
cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,temp,temp_range,R);
%enthalpy Calculation
H = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,temp,temp_range,R);
%Entropy Calculation
S = entropy_1(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,temp,temp_range,R);
% Molecular Weight Calculation
m_weight = weight(spacies_name);
%Saving molecular weight
spacies_w = fopen('species_mass.txt','w');
fprintf(spacies_w,'molecular mass of %s is %f',spacies_name,m_weight);
fclose(spacies_w);
%Plotting and Saving
cur_dir = pwd;
%making a new folder for each species
mkdir(spacies_name);
cd(spacies_name);
figure(1)
plot(temp_range,cp,'linewidth',2,'color',[0.2345 0.4567 0.5678])
xlabel('Temperature(K)');
ylabel('Specific Heat(KJ/kg-k)');
title(spacies_name);
figure(2)
plot(temp_range,H,'linewidth',2,'color',[0.5678 0.3423 0.7874])
xlabel('Temperature(K)');
ylabel('Enthalpy(KJ/Kg)');
title(spacies_name);
figure(3)
plot(temp_range,S,'linewidth',2,'color',[0.4532 0.1234 0.3324])
xlabel('Temperature(K)');
ylabel('Entropy(KJ/K)');
title(spacies_name);
%Saving the plots in seperate file foe each spacies
saveas(1,'specific_heat.jpg');
saveas(2,'enthalpy.jpg');
saveas(3,'entropy.jpg');
cd(cur_dir);
%printing the molecular weight of the spacies
sprintf('molecular weight of %s=%f',spacies_name,m_weight)
end
%closing file
fclose(f1)
SPECIFIC HEAT FUNCTION PROGRAM :
function[cp] = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,temp,temp_range,R)
for i=1:length(temp_range)
if temp_range(i)<=temp(2)
cp(i)=R*(a8 + (a9*temp_range(i)) + (a10*(temp_range(i).^2)) + (a11*(temp_range(i).^3)) + (a12*(temp_range(i).^4)));
else
cp(i)=R*(a1 + (a2*temp_range(i)) + (a3*(temp_range(i).^2)) + (a4*(temp_range(i).^3)) + (a5*(temp_range(i).^4)));
end
end
ENTHALPY FUNCTION PROGRAM :
function [H] = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,temp,temp_range,R)
for i=1:length(temp_range)
if temp_range(i)<=temp(2)
H(i)= R*((a8*temp_range(i)) + (a9*((temp_range(i).^2)/2)) + (a10*((temp_range(i).^3)/3)) + (a11*((temp_range(i).^4)/4)) + (a12*((temp_range(i).^5)/5)) + a13);
else
H(i)= R*((a1*temp_range(i)) + (a2*((temp_range(i).^2)/2)) + (a3*((temp_range(i).^3)/3)) + (a4*((temp_range(i).^4)/4)) + (a5*((temp_range(i).^5)/5)) + a6);
end
end
end
ENTROPY FUNCTION PROGRAM :
function[S] = entropy_1(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,temp,temp_range,R)
for i=1:length(temp_range)
if temp_range(i)<=temp(2)
S(i)= R.*((a8*log(temp_range(i))) + (a9*temp_range(i)) + (a10*((temp_range(i).^2)/2)) + (a11*((temp_range(i).^3)/3)) + (a12*((temp_range(i).^4)/4)) + a14);
else
S(i)= R.*((a1*log(temp_range(i))) + (a2*temp_range(i)) + (a3*((temp_range(i).^2)/2)) + (a4*((temp_range(i).^3)/3)) + (a5*((temp_range(i).^4)/4)) + a7);
end
end
end
MOLECULAR WEIGHT FUNCTION PROGRAM :
function [m_weight] = weight(spacies_name)
element = {'H','O','N','Ar','C','S'};
atomic_mass = [1.008,15.999,14.007,39.948,12.011,32.065];
m_weight=0;
for i=1:length(spacies_name)
for j=1:length(element)
if strcmp(spacies_name(i),element(j))
m_weight = m_weight + atomic_mass(j);
a=j;
end
end
b = str2num(spacies_name(i));
if b>1
m_weight = m_weight + atomic_mass(a)*(b-1);
end
end
end
OUTPUT :
PLOT O2 , N2 , CO2 SPECIES









SCREENSHOT OF FOLDER WHERE ALL SPECIES ARE SAVED

ERROR & SOLUTION :

- So, the first error i found is shown in image, after examine the programe i founnd that i forgot the 'Space' in line 13 (mention in yelllow color)
- Second Error i faces is i used two different spelling for species like 'spacies' & 'species' ,
REFERENCE :