|| Parsing NASA Thermodynamic Data ||
- AIM
NASA came up with polynomials that can be used to evaluate thermodynamic properties such as Cp, H, and S. They have also documented the coefficients that are required to evaluate these polynomials for 1000+ species. In this project, we are going to Parsing this data file for 53 species. Our aim is to extract necessary information from this file and evaluate them to plot the properties of these species. The process of extracting information from any file is known as File Parsing.
- GOVERNING EQUATIONS:
In this Project, we are going to calculate Specific Heat( `C_p` ), Enthalpy( `H` ), Entropy( `S` ) of the 53 species recorded on the file.
File link is - https://drive.google.com/file/d/1AikCHDlz9s_fu8whE81qee_GD4AyYpCP/view?usp=sharing
~ The equation for Specific Heat (`C_p`) - ` C_p/R = a_1 + a_2*T + a_3*T^2 + a_4*T^3 + a_5*T^4 ` --------------------(i)
~ The equation for Enthalpy (`H`) - ` H/(R*T) = a_1 + (a_2*T)/2 + (a_3*T^2)/3 + (a_4*T^3)/4 + (a_5*T^4)/5 + a_6/T` ---------------------(ii)
~ The equation for Entropy (`S`) - ` S/R = a_1*lnT + a_2*T + (a_3*T^2)/2 + (a_4*T^3)/3 + (a_5*T^4)/4 + a_7` --------------------(iii)
Where, `R` is Universal Gas Constant , `T` is Temperature and `a_1,a_2,..,a_7` are coefficients.
- OBJECTIVE:
In this project, we are going to Parsing the NASA Thermodynamic Data & calculate the Properties of the species in the data file. The objectives are-
1) To write a function that extracts the 14 coefficients and calculates the enthalpy, entropy, and specific heats for all the species in the data file.
2) To calculate the molecular weight of each species and display it in the command window.
3) To plot the Cp, Enthalpy, and Entropy for the local temperature range (low temperature: high temperature) specific for each species.
4) To save the plots as images with appropriate names and dump them in separate folders for each species with a suitable name. All of these should be generated automatically.
- Evaluation Of Program:
The program of this project is divided into three parts, one is to write a program to calculate `C_p`, `H`, `S`, 2nd is to calculate Molecular Weight Of the species and 3rd is to extract all the coefficients from the file and to plot `C_p`, `H`, `S` with temperature ranges. The 3rd part is the Main program that includes the evaluation of plots with making folders and saves individual plots within these folders.
1st part of the program to create a Function to calculate `C_p`, `H`, `S` -
% Function To Calculate Cp,Enthalpy and Entropy
% for the local temperature range, specific for each species.
function [ Cp H S ]=Property(A1,A2,A3,A4,A5,A6,A7,a1,a2,a3,a4,a5,a6,a7,T,G_mid_T)
% Cp = Specific Heat of species at constant Pressure
% H = Enthalpy
% S = Entropy
% T = Temperature Range
% G_mid_T = Global Mid Temperature
R = 8.314 ; % R = Universal Gas Constant
for q = 1: length(T)
if T(q) >= G_mid_T
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)).*(T.*R);
S = ((A1.* log(T))+(A2.*T)+(A3.*T.^2)+(A4.*T.^3)+(A5.*T.^4)+A7)* R ;
else
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)).*(T.*R);
S = ((a1.*log(T))+(a2.*T)+(a3.*T.^2)+(a4.*T.^3)+(a5.*T.^4)+a7)* R ;
end
end
endfunction
2nd part of the program to create a Function to calculate Molecular Weight of The species -
% Function To Calculate Molecular Weight
function MW= Molecular_Weight(species)
atoms={'H','C','N','O','A'};
% H=HYDROGEN(1), C=CARBON(12), N=NITROGEN(14), O=OXYGEN(16), A=ARGON(40)
atomic_weight=[1 12 14 16 40];
MW=0;
for i=1:length(species)
for j=1:length(atomic_weight)
if (strcmp(species(i),atoms(j))==1) % for comparing atoms of the species with atoms
MW = MW + atomic_weight(j);
rank=j; % to find the position of atoms in the species
end
end
% For the weights of the repeating atoms
m=str2num(species(i));
if m>1
n=m-1;
MW=MW+atomic_weight(rank)*(n);
end
end
end
3rd part of the program -
clc
clear all
close all
% Program for Parsing NASA thermodynamic data
file = fopen('THERMO.dat','r'); % To open the data file & set to read mode
file_name = fgetl(file) ; % To read the Header line(file name)
first_line = fgetl(file) ; % To read the 1st line after Header
Global_Temp_Range=strsplit(first_line,' ') ;% To extract temp from 1st line
G_low_T = str2num(Global_Temp_Range{2}); % To get the Global low temperature
G_mid_T = str2num(Global_Temp_Range{3}) ; % To get the Global mid temperature
G_high_T = str2num(Global_Temp_Range{4}) ;% To get the Global high temperature
for i= 1:3
a= fgetl(file) ; % To read the next 3 lines after first line
end
for j= 1:53
line_1 = fgetl(file); %To read 1st line of species,as ranked 1 on RHS of data file
L1 = strsplit(line_1,' '); % To split 1st line
species = (L1{1}); % To extract Species from 1st line
G = strfind(line_1,'G '); % To find the position of G in 1st line
L_low_T = str2num(line_1(G+3:G+10)); % To extract LOCAL Low Temperature
L_high_T = str2num(line_1(G+12:G+20)); % To extract LOCAL High Temperature
L_mid_T = str2num(line_1(G+22:G+30));% To extract LOCAL Midium Temperature
T = linspace(L_low_T,L_high_T,500); % To set a range of Local Temperature
line_2 = fgetl(file); %To read 2nd line of species,as ranked 2 on RHS of data file
L2 = strfind(line_2,'E'); % To find the position of E in 2nd line
A1 = str2num(line_2(1:L2(1)+3)); % To extract 1st coefficient for High Temperature
A2 = str2num (line_2(L2(1)+4:L2(2)+3)); % To extract 2nd coefficient for High Temperature
A3 = str2num (line_2(L2(2)+4:L2(3)+3)); % To extract 3rd coefficient for High Temperature
A4 = str2num (line_2(L2(3)+4:L2(4)+3)); % To extract 4th coefficient for High Temperature
A5 = str2num (line_2(L2(4)+4:L2(5)+3)); % To extract 5th coefficient for High Temperature
line_3 = fgetl(file); %To read 3rd line of species,as ranked 3 on RHS of data file
L3 = strfind(line_3,'E'); % To find the position of E in 3rd line
A6 = str2num(line_3(1:L3(1)+3)); % To extract 6th coefficient for High Temperature
A7 = str2num (line_2(L3(1)+4:L3(2)+3)); % To extract 7th coefficient for High Temperature
a1 = str2num (line_2(L3(2)+4:L3(3)+3)); % To extract 1st coefficient for Low Temperature
a2 = str2num (line_2(L3(3)+4:L3(4)+3)); % To extract 2nd coefficient for Low Temperature
a3 = str2num (line_2(L3(4)+4:L3(5)+3)); % To extract 3rd coefficient for Low Temperature
line_4 = fgetl(file); %To read 4th line of species,as ranked 4 on RHS of data file
L4 = strfind(line_4,'E'); % To find the position of E in 4th line
a4 = str2num(line_4(1:L4(1)+3)); % To extract 4th coefficient for Low Temperature
a5 = str2num (line_4(L4(1)+4:L4(2)+3)); % To extract 5th coefficient for Low Temperature
a6 = str2num (line_4(L4(2)+4:L4(3)+3)); % To extract 6th coefficient for Low Temperature
a7 = str2num (line_4(L4(3)+4:L4(4)+3)); % To extract 7th coefficient for Low Temperature
% Function To Calculate Cp,Enthalpy and Entropy
[Cp,H,S] = Property(A1,A2,A3,A4,A5,A6,A7,a1,a2,a3,a4,a5,a6,a7,T,G_mid_T);
% To creat Folders with species name
Folder_name = sprintf('%d) [%s]',j, species); % To give Folder names
current_dir = pwd ; % To specify the current directory
% All The Function Program And Thermodynamic Data file are stored in a folder,that specify current directory
mkdir(Folder_name); % To creat a Folder
cd(Folder_name); % To load plots in that new folder
figure (1);
plot(T,Cp,'r','linewidth',3)
grid on
xlabel(' Local Temperature');
ylabel(' Specific Heat')
title(' Specific Heat(Cp) Vs Temperature(T) ')
IMAGE1 = sprintf(" Cp Vs T of [%s].png",species); % To give a name of the file
saveas(gca,IMAGE1) % To save the image file
figure (2);
plot(T,H,'b','linewidth',3)
grid on
xlabel(' Local Temperature');
ylabel(' Enthalpy')
title(' Enthalpy(H) Vs Temperature(T) ')
IMAGE2 = sprintf(" H Vs T of [%s].png",species); % To give a name of the file
saveas(gca,IMAGE2) % To save the image file
figure (3);
plot(T,S,'g','linewidth',3)
grid on
xlabel(' Local Temperature');
ylabel(' Entropy')
title(' Entropy(S) Vs Temperature(T) ')
IMAGE3 = sprintf(" S Vs T of [%s].png",species); % To give a name of the file
saveas(gca,IMAGE3) % To save the image file
cd(current_dir); % To give a path,return to current directory
% Function To Calculate Molecular Weight
MW = Molecular_Weight(species);
fprintf(' %d) Molecular Weight of [%s] is %d.',j,species,MW) % To display as output
fprintf('\n \n') % To set the next output after 2 line interval
endfor
All these steps of the program are described along with the codes. Some of the Functions are described bellow-
- strcmp(s1,s2) ~ To determine if two strings(s1,s2) are identical. It compares complete strings.
- fopen('file-name','mode') ~ To open any file and to select a mode of operation for the file. Modes are read(r), write(w).
- fgetl(file-name) ~ To Read characters from a file, stopping after a newline.
- str2num(s) ~ To convert a string (character) to number.
- pwd ~ Return the current working directory.
- mkdir(folder-name) ~ To create a folder(directory).
- cd(folder-name) ~ To specify that files(plots, .png, etc) should be stored in the specified folder.
- gca ~ Return a handle to the current axes object.
- saveas(figure,file-name) ~ To save a figure(gca can be used) as file.
- fprintf(' title %s %d ',string,number) ~ fprintf means "File PRINT Formatted": use this for printing text to a file on the command window.
- sprintf(' title %s %d ',string,number) ~ sprintf means "String PRINT Formatted": use this for creating a MATLAB string ( character vector ).
OUTPUTS -
Plots of the [ O2 ] -
Plots of the [ N2 ] -
Plots of the [ CO2 ] -
Molecular weight on the Command Window -
Screenshot of the window where all the folders are saved -
ERRORS -
A series of Errors have been faced during programming. Some of the errors are related to the code of molecular weight. We must check for repeating atoms of the species.
It is the wrongly written so that it gives the output as -
After that, it is corrected, as - Here, we should check every atom of the species specifically. So we use " species(i) " to check every atom of the species for every iteration.
Some other Errors are related to finding the position of "G" in the NASA Thermodynamic file, as the position of "G" can be used to extract the values of Local Temperatures from the file. During this operation, it gives the wrong result for the species [CH2OH], as there are two G's in this line. So, to be specific, we use "G " instead of "G", now we find the position of "G(space)" to eliminate this confusion.
Next Error occurred during saving the plots in the respective species folders. We must use a command to return the working directory to the main directory where all the functions are saved. So we must use "cd" command for that.
- CONCLUSION:
After observing these plots, we understand that the Specific heat of any species is not a constant value, it depends on temperature. Generally, we use constant values for our calculation, but we conclude that the Specific Heat, Enthalpy, Entropy are temperature-dependent variables. NASA has determined these polynomial values to calculate these properties of each species. In this project, we evaluate them with the help of the PARSING data file along with we write a program to calculate the molecular weight of each 53 species and creat individual folders to store the respective plots.
- REFERENCES:
- https://www.mathworks.com/matlabcentral/answers/16009-difference-between-sprintf-and-fprintf#:~:text=sprintf()%20returns%20the%20formatted,port)%20it%20is%20connected%20to.
- https://skill-lync.com/challenges/question/Um1wL1ZTVUhVWklqbnNuNzZhSjNZZz09Ojp5cmU1bDZWWXZxSnRWNWNI/dlpuVGlxNjlhZVd3MGlXTW1mVWI0UT09OjpBaGlkcDZoNnlnaTRGU3V6/SUtDT1BVRHdCZjYwL0pacUI2eExIQT09OjpLVUx4WFFUdlp6Z2tIS1Nh