Parsing NASA Thermodynamic Data using MATLAB
AIM :
To Parse the Thermodynamic Data from NASA using MATLAB
THEORY :
Data Parsing
Data parsing is a process of extracting data from a file and use that data for further calculation or study or making some important conclusions and how that information can be used in a computer program.
NASA Thermodynamic Data File
'THERMO.dat' file is a data file containing 14 coefficients for 53 different elements given by NASA. These coefficients are utilized to calculate the specific heat, enthalpy, and entropy for a particular element. Each element has its own range of local temperature.
GOVERNING EQUATIONS :
- Specific Heat
`C_P⁄R =a1+a2 ×T+a3 × T^2+ a4 × T^3+ a5 ×T^4`
- Enthalpy
`H⁄RT =a1+a2 × T⁄2+ a3 × T^2⁄3+ a4 × T^3⁄4+ a5 × T^4⁄5+ a6⁄T`
- Entropy
`S⁄R =a1 × lnT+ a2 ×T+a3 × T^2⁄2+ a4 × T^3⁄3+ a5 × T^4⁄4+ a7`
where,
CP = Specific Heat
H = Enthalpy
S = Entropy
R = Universal Gas Constant
T = Temperature
* The First 7 Coefficients are High Temperature Coefficients and the Second 7 Coefficients are Low Temperature Coefficients.
OBJECTIVE :
- To write a function extracting the 14 coefficients from the data file and calculating the specific heat, enthalpy and entropy of all species.
- To calculate molecular weight of each species and displaying in command window.
- To plot specific heat, enthalpy and entropy for the local temperature range (low temperature : high temperature) specific for each species.
- To create a separate folder for all the plots and saving them in ‘.png’ format.
MAIN CODE :
% Program to Parse NASA's Thermodynamic Data
close all
clear
clc
% Universal Gas Constant (R)
R = 8.314;
% Loading Data File
File = fopen('THERMO.dat','r');
% Reading 1st Line
fgetl(File);
% Defining Temperature Values
t_line = fgetl(File);
Global_Temp = strsplit(t_line,' ');
Global_Low_Temp = str2double(Global_Temp{2});
Global_Mid_Temp = str2double(Global_Temp{3});
Global_High_Temp = str2double(Global_Temp{4});
% For Skipping Unwanted Lines
for i = 1:3
comments = fgetl(File);
end
% Creating a New Folder
mkdir('F:\MATLAB\Program\Parsing NASA Thermodynamic Data','SPECIES')
% To Iterate through all 53 Elements
for i = 1:53
% Extracting Species Name
line1 = fgetl(File);
Species = Get_Species(line1);
% Calculating Molecular Weight
Molecular_Weight = Molecular_weight(Species);
fprintf('\nMolecular Weight of %s is : %d',Species,Molecular_Weight)
% Calculating Local Temperature Range
Local_Temp_Range = Get_Local_Temp_Range(line1);
% Calculating Coefficients Data
c(1:5) = Coefficients(fgetl(File));
c(6:10) = Coefficients(fgetl(File));
c(11:14) = Coefficients(fgetl(File));
% Temperature
T = linspace(Local_Temp_Range(1),Local_Temp_Range(3),500);
% Specific Heat ( Cp )
Cp = Specific_Heat(c, Local_Temp_Range, T, R);
% Enthalpy ( H )
H = Enthalpy(c, Local_Temp_Range, T, R);
% Entropy ( S )
S = Entropy(c, Local_Temp_Range, T, R);
% Plotting and Saving
% Creating New Folder with Species Name
cd('SPECIES')
mkdir(Species);
% Open that Folder
cd(Species)
% Plotting Temperature VS Specific Heat
figure(1)
plot(T,Cp,'linewidth',2,'color','k')
xlabel('Temperature [K]');
ylabel('Specific Heat [KJ/Kg-K]');
title(sprintf('Temperature VS Specific Heat Plot for %s',Species))
grid on
% Saving Plots
saveas(figure(1), 'Specific Heat.png')
% Plotting Temperature VS Enthalpy
figure(2)
plot(T,H,'linewidth',2,'color','r')
xlabel('Temperature [K]');
ylabel('Enthalpy [KJ/Kg]');
title(sprintf('Temperature VS Enthalpy Plot for %s',Species))
grid on
% Saving Plots
saveas(figure(2), 'Enthalpy.png')
% Plotting Temperature VS Entropy
figure(3)
plot(T,S,'linewidth',2,'color','g')
xlabel('Temperature [K]');
ylabel('Entropy [KJ/Kg-K]');
title(sprintf('Temperature VS Entropy Plot for %s',Species))
grid on
% Saving Plots
saveas(figure(3), 'Entropy.png')
% Goes Back to Parent Folder
cd('F:\MATLAB\Program\Parsing NASA Thermodynamic Data')
end
Explanation
Step 1 : Defined the input value of Universal Gas Constant (R).
Step 2 : Then, "THERMO.dat" file was opened using "fopen" command. And the lines of this file were read and extracted by using the "fgetl" command.
Step 3 : Because the next three lines in the file do not have any information regarding the code. The lines were skipped. And then, created a new folder called "SPECIES" .
Step 4 : Then as there are 53 species so "for" loop was run "53" times. Now before solving the main code, we have to create some functions.
Function for Calculating Species :
function Species = Get_Species(line)
split = strsplit(line,' ');
Species = (split{1});
end
Step 5 : So firstly, we created a function called "Get_Species". Then, we called the function in our main code to getting species name.
Function for Calculating Molecular Weight :
function molecular_weight = Molecular_weight(species)
molecular_weight = 0;
Atoms = ['O', 'H', 'N', 'C', 'S', 'A'];
Atomic_weights = [16, 1, 14, 12, 32, 40];
for i = 1 : length(species)
for j = 1 : length(Atoms)
if strcmp(species(i), Atoms(j))
molecular_weight = molecular_weight + Atomic_weights(j);
w = j;
end
end
z = str2double(species(i));
if z > 1
molecular_weight = molecular_weight + Atomic_weights(w) * (z-1);
end
end
end
Step 6 : Then, we created a function called "Molecular_weight". And then, we called the function in our main code to solve the molecular weight.
Function for Calculating Local Temperature Range :
function temp = Get_Local_Temp_Range(line)
split = strsplit(line, ' ');
l = length(split);
low_temp = str2double(split{l-3});
mid_temp = str2double(split{l-1});
high_temp = str2double(split{l-2});
temp = [low_temp mid_temp high_temp];
end
Step 7 : And then, we created a function called "Get_Local_Temp_Range" for getting local temperature range.
Function for Calculating Coefficients Data :
function c = Coefficients(line)
E = strfind(line,'E');
c(1) = str2double(line(1 : E(1)+3));
for i = 1 : length(E)-1
t = str2double(line(E(i)+4 : E(i+1)+3));
c(i+1) = t;
end
end
Step 8 : Then, we created a function called "Coefficients" for calculating the coefficients data.
Function for Calculating Specific Heat :
function Cp = Specific_Heat(c, local_temp_range, T, R)
for i = 1 : length(T)
if T(i) <= local_temp_range(2)
Cp(i) = R*(c(8) + c(9)*T(i) + c(10)*T(i)^2 + c(11)*T(i)^3 + c(12)*T(i)^4);
else
Cp(i) = R*(c(1) + c(2)*T(i) + c(3)*T(i)^2 + c(4)*T(i)^3 + c(5)*T(i)^4);
end
end
end
Step 9 : Now, we created a function called "Specific_Heat". Then, we called the function in our main code to solve the specific heat.
Function for Calculating Enthalpy :
function H = Enthalpy(c, local_temp_range, T, R)
for i = 1 : length(T)
if T(i) <= local_temp_range(2)
H(i) = R*T(i)*(c(8) + c(9)*T(i)/2 + c(10)*(T(i)^2)/3 + c(11)*(T(i)^3)/4 + c(12)*(T(i)^4)/5 + c(13)/T(i));
else
H(i) = R*T(i)*(c(1) + c(2)*T(i)/2 + c(3)*(T(i)^2)/3 + c(4)*(T(i)^3)/4 + c(5)*(T(i)^4)/5 + c(6)/T(i));
end
end
end
Step 10 : Again, we created a function called "Enthalpy". And then, we called the function in our main code to solve the enthalpy.
Function for Calculating Entropy :
function S = Entropy(c, Local_Temp_Range, T, R)
for i = 1 : length(T)
if T(i) <= Local_Temp_Range(2)
S(i) = R*(c(8)*log(T(i)) + c(9)*T(i) + c(10)*(T(i)^2)/2 + c(11)*(T(i)^3)/3 + c(12)*(T(i)^4)/4 + c(14));
else
S(i) = R*(c(1)*log(T(i)) + c(2)*T(i) + c(3)*(T(i)^2)/2 + c(4)*(T(i)^3)/3 + c(5)*(T(i)^4)/4 + c(7));
end
end
end
Step 11 : Lastly, we created a function called "Entropy" to solve the entropy.
Step 12 : At the end, we plotted for Temperature VS ( Specific Heat, Enthalpy and Entropy ) respectively for each and every element and saved them in separate folders.
RESULTS :
Workspace Values :
Molecular Weight of Each Species :
Folders that were Created Automatically :
Plots of O2 :
Plots of N2 :
Plots of CO2 :