PARSING NASA THERMODYNAMIC DATA
AIM: Matlab program to parse the thermodynamic data from NASA
THEORY:
'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. or Parsing is the act of splitting up information into its component parts.
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 transformation on the data.
Parsing in MATLAB means reading an m-file and translating it into its components parts that stores it in memory. When we execute the code , the parsed file then run which is stored in memory.
Thermodynamic data- NASA came up with polynomials that can be used to evaluate thermodynamic properties such as Specific heat, Enthalpy and Entropy. They have also documented the co-efficients that are required to evaluate these polynomials for 1000+ species.
GOVERNING EQUATION:
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 (8.314 J/mol.K)
T = Temperature
** The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients.
OBJECTIVE:
- To write a function that extracts the 14 co-efficient and calculating the enthalpy, entropy, and specific heats for all the species in the data file.
- To calculate the molecular weight of each species and displaying it in the command window.
- To plot the Specific heat, Enthalpy and Entropy for the local temperature range (low temperature: high temperature) specific for each species.
- To save the plots as image (‘.png’ format) and saving them in separate folders for each species.
INPUT:
https://drive.google.com/file/d/1AikCHDlz9s_fu8whE81qee_GD4AyYpCP/view?usp=sharing
FUNCTION PROGRAM 1: Calculating the thermodynamic properties (specific heat, enthalpy, entropy).
function [cp, h, s] = coefficient(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T)
R = 8.314; % Universal gas constant
for j = 1 :length(T)
if T(j)<300
cp = (a1 + a2.*T + a3.*T.^2 + a4.*T.^3 + a5.*T.^4).*R;
h = (a1 + (a2.*T)./2 + (a3.*T.^2)./3 + (a4.*T.^3)./4 + (a5.*T.^4)./5 + (a6./T)).*R.*T;
s = (a1.*log(T) + a2.*T + (a3.*T.^2)./2 + (a4.*T.^3)./3 + (a5.*T.^4)./4 + a7).*R;
else
cp = (a8 + a9.*T + a10.*T.^2 + a11.*T.^3 + a12.*T.^4).*R;
h = (a8 + (a9.*T)./2 + (a10.*T.^2)./3 + (a11.*T.^3)./4 + (a12.*T.^4)./5 + (a13./T)).*R.*T;
s = (a8.*log(T) + a9.*T + (a10.*T.^2)./2 + (a11.*T.^3)./3 + (a12.*T.^4)./4 + a14).*R;
end
end
end
FUNCTION PROGRAM 2: Calculating the molecular weight of each species mentioned in the data file.
function [molecular_weight] = mol_wt(species_name)
SPECIES = (species_name);
constituents = {'H','C','N','O','A'};
atomic_weight = [1 12 14 16 40];
molecular_weight = 0;
for i = 1:length(SPECIES)
for j = 1:length(constituents)
if strcmp(SPECIES(i) , constituents(j))
molecular_weight = molecular_weight + atomic_weight(j);
u = j;
end
end
n = str2double(SPECIES(i));
if n > 1
molecular_weight = molecular_weight + atomic_weight(u)*(n -1);
end
end
fprintf('molecular_weight of %s: ' ,species_name)
fprintf('%d', molecular_weight)
fprintf(' ')
fprintf('amu')
fprintf('\n')
end
MAIN PROGRAM:
% Program to parse the thermodynamic data
clear all
close all
clc
% Reading the thermodynamic data file
f1 = fopen('THERMO.dat','r');
file_name = fgetl(f1);
t_line = fgetl(f1);
A = strsplit(t_line,' ');
% Defining the temperature extracted from data file
global_low_temp = str2double(A{2});
global_mid_temp = str2double(A{3});
global_high_temp = str2double(A{4});
% For skipping the comments lines
for i = (1:3)
comments = fgetl(f1);
end
for i = 1:53
% Extracting species name
species = fgetl(f1);
species_name = species(1:6);
% Extracting coefficients from first line
line_1 = fgetl(f1);
l1 = strfind(line_1,'E');
a1 = str2double(line_1(1:(l1(1) + 3)));
a2 = str2double(line_1((l1(1)+4):(l1(2) + 3)));
a3 = str2double(line_1((l1(2)+4):(l1(3) + 3)));
a4 = str2double(line_1((l1(3)+4):(l1(4) + 3)));
a5 = str2double(line_1((l1(4)+4):(l1(5) + 3)));
% Extracting coefficients from second line
line_2 = fgetl(f1);
l2 = strfind(line_2,'E');
a6 = str2double(line_2(1:(l2(1) + 3)));
a7 = str2double(line_2((l2(1)+4):(l2(2) + 3)));
a8 = str2double(line_2((l2(2)+4):(l2(3) + 3)));
a9 = str2double(line_2((l2(3)+4):(l2(4) + 3)));
a10 = str2double(line_2((l2(4)+4):(l2(5) + 3)));
% Extracting coefficients from third line
line_3 = fgetl(f1);
l3 = strfind(line_3,'E');
a11 = str2double(line_3(1:(l3(1) + 3)));
a12 = str2double(line_3((l3(1)+4):(l3(2) + 3)));
a13 = str2double(line_3((l3(2)+4):(l3(3) + 3)));
a14 = str2double(line_3((l3(3)+4):(l3(4) + 3)));
% Creating a folder 'SPECIES' to save thermodynamic plots of species
mkdir(['E:\Project NASA Parsing\species\', species_name]);
T = linspace(global_low_temp, global_high_temp, 500);
% Calling the thermodynamic equations function
[cp, h, s] = coefficient(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T);
% Plotting
% Plot between Temperature and Specific heat
figure(1)
plot(T, cp, 'linewidth', 3 ,'color', 'b')
title(species_name)
xlabel('Temperature')
ylabel('Specific Heat')
% Plot between Temperature and Enthalpy
figure(2)
plot(T, h , 'linewidth', 3 ,'color', 'g')
title (species_name)
xlabel('Temperature')
ylabel('Enthalpy')
% Plot between Temperature and Entropy
figure(3)
plot(T, s , 'linewidth', 3 ,'color', 'c')
title (species_name)
xlabel('Temperature')
ylabel('Entropy')
directory = (['E:\Project NASA Parsing\species\', species_name]);
cd(directory)
% Saving plots
saveas(1, 'specific_heat.png')
saveas(2, 'enthalpy.png')
saveas(3, 'entropy.png')
% Changing directory for calculating molecular weight
cd('C:\Users\mms\Desktop\matlab\challenges')
molecular_weight = mol_wt(species_name);
end
fclose(f1)
EXPLANATION OF THE CODE:
For the function program:
Initially, a function program is written to calculate the thermodynamic properties.
Another function is also written which calculate the molecular weight.
For the main program:
- In the 1st step, we open the THERMO.dat file for reading the data by using the command fopen ().
Then, the fgetl () command is used which read the file line by line.
After that we have splitted the string with strsplit () command and space (‘ ’) is used as the delimiter for splitting.
Further we read the header files or comments.
- In the next step we have extracted the species data or coefficient for all the species, with the help of a for loop.
For extracting, we first locate the string ‘E’ first, which is present in coefficient value with the strfind () command.
After finding the location of E, for the 1st coefficient we use. This means that form gives the string E location and gives the value upto string E by adding 3, it will give the whole coefficient value.
Similarly, other coefficients are extracted and str2double command is used to convert the string into number.
- To save the plots separately in a particular folder a command mkdir is used to create a new directory in which we have pasted the proper address location.
Cd means current directory with this plot are saved inside the folder name with specific species input as the folder name. Each folder contains 3 plots
Then by changing the directory, the molecular weight is saved.
- Function is called then to store the values of Cp, h and s.
- Plotting is done for the Temperature vs Specific heat, Temperature vs enthalpy, Temperature vs entropy, respectively.
- Then, plots are saved as an image.
- Molecular weight function is called, and the directory is changed to print the molecular weight of the species in the command window.
OUTPUT 1: Command window listing molecular weight of the species.
OUTPUT 2: (Location of SPECIES folder)
PLOTS:
For `O_2`:
For `N_2`:
For `CO_2`:
ERRORS:
- Small errors include typos and not saving the Thermo data file in the correct folder. So, these errors are corrected by reviewing the program.
- Other errors include of not creating a proper folder and saving the plots in them. These are also corrected.
- And some other warnings are also occured which are solved.
CONCLUSION:
In this project, the NASA thermodynamic data file is parsed to extract useful information from the file, like species coefficients, temperature ranges using MATLAB. The data extracted is used to calculate the Specific Heat, Enthalpy and Entropy of a species in its temperature range and respective graphs are plotted. Also, the plots are saved in a separate folder. So from the above program, it can be conclude that new folders with desired information and name can be created and the files can be saved for the further use.
REFERENCES:
https://en.wikipedia.org/wiki/Parsing
https://www.mathworks.com/help/matlab/matlab_prog/parse-function-inputs.html
Mathworks