Project 1 - Parsing NASA thermodynamic data
AIM
To write a code in MATLAB to parse the NASA thermodynamic data file and calculate the thermodynamic properties of various gas species.
OBJECTIVE
To write a function that extracts the 14 co-efficients and calculates the specific heat, enthalpy and entropy for all the species in the data file.
To calculate the molecular weight of each species and display it in the command window.
To plot Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.
To save the plots as images with appropriate names and dump them in separate folders for each species with suitable name and all these should be done automatically.
The main program will just take the 'THERMO.dat' file as input and generate Cp, H and S plots.
File Parsing
It is the method of splitting the file or other input into a piece of data that can be easily stored or manipulated. By doing this we are going to split a string into parts then recognizing the parts to convert it into something simpler than a string.
Here in this project we are going to split the NASA thermodynamic data which is having 53 gas species values in 213 lines. To perform the calculation for thermodynamic properties, we need some sort of co-efficient values and temperature ranges which is available in the NASA file. So, we are going to parse the values which are all needed for our calculation from that file.
Specific Heat
Specific heat is the amount of heat energy required to raise the temperature of body per unit of mass. Specific heat is also known as specific heat capacity or mass specific heat. In SI units, specific heat is the amount of heat in joules required to raise 1 gram of a substance 1 Kelvin.
Enthalpy
When a substance changes at constant pressure, enthalpy tells how much heat and work was added or removed from the substance. Enthalpy is similar to energy but not the same. When a substance grows or shrinks, energy is used up or released.
Entropy
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 of atoms a system can have. In this sense, entropy is the measure of uncertainty or randomness.
Gas Constant
The gas constant is a physical constant denoted by R and is expressed in terms of units of energy per temperature increment per mole. The gas constant value is equivalent to Boltzmann constant but expressed as the pressure-volume product instead of nergy per increment of temperature per particle.
Molecular Weight
Molecular weight is a measure of the sum of the atomic weight values of the atoms in a molecule. Molecular weight is used in chemistry to determine stoichiometry in chemical reactions and equations.
EQUATION
Cp/R = a1 + a2 T + a3 T^2 + a4 T^3 + a5 T^4
H/RT = a1 + a2 T /2 + a3 T^2 /3 + a4 T^3 /4 + a5 T^4 /5 + a6/T
S/R = a1 lnT + a2 T + a3 T^2 /2 + a4 T^3 /3 + a5 T^4 /4 + a7
where,
Cp = Specific Heat (J/K kg)
H = Enthalpy (J)
S = Entropy (J/K)
R = Universal gas constant = 8.314 (J/mol.K)
T = Temperature (K)
MAIN CODE
clear
close
clc
% Program for Parsing NASA thermodynamic data
% For opening the data file & set to read mode
file = fopen('THERMO.dat','r');
% To read the Header line(file name)
file_name = fgetl(file) ;
% To read the 1st line after Header
first_line = fgetl(file) ;
% To extract temp from 1st line
Global_Temp_Range=strsplit(first_line,' ');
% To get the Global low temperature
G_low_T = str2num(Global_Temp_Range{2});
% To get the Global mid temperature
G_mid_T = str2num(Global_Temp_Range{3});
% To get the Global high temperature
G_high_T = str2num(Global_Temp_Range{4});
for i= 1:3
a= fgetl(file) ; % To read the next 3 lines after first line
end
for j= 1:53
%To read 1st line of species,as ranked 1 on RHS of data file
line_1 = fgetl(file);
% To split 1st line
L1 = strsplit(line_1,' ');
% To extract Species from 1st line
species = (L1{1});
% To find the position of G in 1st line
G = strfind(line_1,'G ');
% To extract LOCAL Low Temperature
L_low_T = str2num(line_1(G+3:G+10));
% To extract LOCAL High Temperature
L_high_T = str2num(line_1(G+12:G+20));
% To extract LOCAL Midium Temperature
L_mid_T = str2num(line_1(G+22:G+30));
% To set a range of Local Temperature
T = linspace(L_low_T,L_high_T,500);
%To read 2nd line of species,as ranked 2 on RHS of data file
line_2 = fgetl(file);
% To find the position of E in 2nd line
L2 = strfind(line_2,'E');
% To extract 1st coefficient for High Temperature
A1 = str2num(line_2(1:L2(1)+3));
% To extract 2nd coefficient for High Temperature
A2 = str2num (line_2(L2(1)+4:L2(2)+3));
% To extract 3rd coefficient for High Temperature
A3 = str2num (line_2(L2(2)+4:L2(3)+3));
% To extract 4th coefficient for High Temperature
A4 = str2num (line_2(L2(3)+4:L2(4)+3));
% To extract 5th coefficient for High Temperature
A5 = str2num (line_2(L2(4)+4:L2(5)+3));
%To read 3rd line of species,as ranked 3 on RHS of data file
line_3 = fgetl(file);
% To find the position of E in 3rd line
L3 = strfind(line_3,'E');
% To extract 6th coefficient for High Temperature
A6 = str2num(line_3(1:L3(1)+3));
% To extract 7th coefficient for High Temperature
A7 = str2num (line_2(L3(1)+4:L3(2)+3));
% To extract 1st coefficient for Low Temperature
a1 = str2num (line_2(L3(2)+4:L3(3)+3));
% To extract 2nd coefficient for Low Temperature
a2 = str2num (line_2(L3(3)+4:L3(4)+3));
% To extract 3rd coefficient for Low Temperature
a3 = str2num (line_2(L3(4)+4:L3(5)+3));
%To read 4th line of species as ranked 4 on RHS of data file
line_4 = fgetl(file);
% To find the position of E in 4th line
L4 = strfind(line_4,'E');
% To extract 4th coefficient for Low Temperature
a4 = str2num(line_4(1:L4(1)+3));
% To extract 5th coefficient for Low Temperature
a5 = str2num (line_4(L4(1)+4:L4(2)+3));
% To extract 6th coefficient for Low Temperature
a6 = str2num (line_4(L4(2)+4:L4(3)+3));
% To extract 7th coefficient for Low Temperature
a7 = str2num (line_4(L4(3)+4:L4(4)+3));
% Function To Calculate Cp,Enthalpy and Entropy
[Cp,H,S] = func(A1,A2,A3,A4,A5,A6,A7,a1,a2,a3,a4,a5,a6,a7,T,G_mid_T);
% To creat Folders with species name
% To give Folder names
Folder_name = sprintf('%d) [%s]',j, species);
% To specify the current directory
current_dir = pwd;
% All The Function Program And Thermodynamic Data file are stored in a folder,that specify current
% To creat a Folder
mkdir(Folder_name);
% To load plots in that new folder
cd(Folder_name);
figure (1);
plot(T,Cp,'b','linewidth',4)
grid on
xlabel('Local Temp');
ylabel('Specific Heat')
title('Specific Heat(Cp) Vs Temperature(T)')
% To give a name of the file
IMAGE1 = sprintf("Cp Vs T of [%s].png",species);
% To save the image file
saveas(gca,IMAGE1)
figure (2);
plot(T,H,'g','linewidth',4)
grid on
xlabel('Temp (Local)');
ylabel('Enthalpy')
title('Enthalpy Vs Temp')
% To give a name of the file
IMAGE2 = sprintf(" H Vs T of [%s].png",species);
% To save the image file
saveas(gca,IMAGE2)
figure (3);
plot(T,S,'r','linewidth',4)
grid on
xlabel('Local Temp');
ylabel('Entropy')
title('Entropy(S) Vs Temp(T)')
% To give a name of the file
IMAGE3 = sprintf("S Vs T of [%s].png",species);
% To save the image file
saveas(gca,IMAGE3)
% To give a path,return to current directory
cd(current_dir);
% Function To Calculate Molecular Weight
MW = mol_wt(species);
% To display as output
fprintf(' %d) Molecular Weight of [%s] is %d.',j,species,MW)
% To set the next output after 2 line interval
fprintf(' ')
end
MOLECULAR WEIGHT
% Function for calculation of Molecular Weight
function MW= mol_wt(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)
% for comparing atoms of the species with atoms
if (strcmp(species(i),atoms(j))==1)
MW = MW + atomic_weight(j);
% to find the position of atoms in the species
rank=j;
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
FUNCTION FOR CALCULATING CP S H
% Function for Calculation of Cp,Entropy & Enthalpy
% for the local temp range, specific for each species
function [ Cp H S ]=func(A1,A2,A3,A4,A5,A6,A7,a1,a2,a3,a4,a5,a6,a7,T,G_mid_T)
% T = Temp. Range
% Cp = Specific Heat of species at constant Pressure
% H = Enthalpy
% G_mid_T = Global Mid Temp.
% S = Entropy
% R = Gas Constant
R = 8.314;
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
EXPLAINATION
The program starts with the clear all close all and clc which clears the previous recorded data and to open the thermal file use command fopen,to read the heading line of the data use fgetl command and to read the next line use same command (fgetl).
Getting the global temperature information
get the global temperature line fgetl command is used ,usually information will stored in the form of strings, to get the numerical value use str2double command which converts the text in string of numerical value in string to double precision value and use {} instead of () which means the global temperature line is in the form of cell array by which we can obtain the global temperature values and to skip the next three lines use fgetl command
local temperature information of individual species
To Extract the coefficient values
To extract the value of coefficient which is used to calculate the specific heat ,enthalpy and entropy use fgetl command.Once we get the coefficient line of thermal data ,observe that letter 'E' is present in each coefficient and use strfind command used to find the string 'E' and store it in as a,b,c and in command window it will show the location of E by which we can extract the first coefficient and it will be in the form of string and to get the numerical value use str2double command by these we can obtain all 14 coefficient.
SPECIFIC HEAT FUNCTION
If loop is used because their is a condition that first 7 coefficient are high temperature coefficient and second 7 coefficient are low temperature coefficient.Hence the function is coded as shown .
ENTHALPY FUNCTION
If loop is used because their is a condition that first 7 coefficient are high temperature coefficient and second 7 coefficient are low temperature coefficient.Hence the function is coded as shown
ENTROPY FUNCTION
From the formula it is observed that Entropy is a function of coefficients,universal gas constant,temperature.If loop is used because their is a condition that first 7 coefficient are high temperature coefficient and second 7 coefficient are low temperature coefficient.Hence the function is coded as shown
Molecular Weight Function
It is observed that the thermodynamic data contain mainly 5 species such as oxygen,Hydrogen,Carbon,Nitrogen,Argon and symbols of these species is stored in array called elements and its corresponding atomic mass is stored in atomic mass array.For loop is initiated because the process is repeated for every species . strcmpi command is used to compare the string character of species name and coresponding elements and molecular weight is calculated using the formula mol wt = mol wt + atomic mass.If their is a repeatation of species then if loop is initiated and molecular weight is calculated
n main codeing we write the above codeing in which sprintf is used to format the molecular weight values into string vector and %f command is used to get the floating value of molecular weight of species.disp(k) is used to display the values of molecular weight in command window.
COMMAND WINDOW
1) Molecular Weight of [O] is 16. 2) Molecular Weight of [O2] is 32. 3) Molecular Weight of [H] is 1. 4) Molecular Weight of [H2] is 2. 5) Molecular Weight of [OH] is 17. 6) Molecular Weight of [H2O] is 18. 7) Molecular Weight of [HO2] is 33. 8) Molecular Weight of [H2O2] is 34. 9) Molecular Weight of [C] is 12. 10) Molecular Weight of [CH] is 13. 11) Molecular Weight of [CH2] is 14. 12) Molecular Weight of [CH2(S)] is 14. 13) Molecular Weight of [CH3] is 15. 14) Molecular Weight of [CH4] is 16. 15) Molecular Weight of [CO] is 28. 16) Molecular Weight of [CO2] is 44. 17) Molecular Weight of [HCO] is 29. 18) Molecular Weight of [CH2O] is 30. 19) Molecular Weight of [CH2OH] is 31. 20) Molecular Weight of [CH3O] is 31. 21) Molecular Weight of [CH3OH] is 32. 22) Molecular Weight of [C2H] is 25. 23) Molecular Weight of [C2H2] is 26. 24) Molecular Weight of [C2H3] is 27. 25) Molecular Weight of [C2H4] is 28. 26) Molecular Weight of [C2H5] is 29. 27) Molecular Weight of [C2H6] is 30. 28) Molecular Weight of [CH2CO] is 42. 29) Molecular Weight of [HCCO] is 41. 30) Molecular Weight of [HCCOH] is 42. 31) Molecular Weight of [H2CN] is 28. 32) Molecular Weight of [HCN] is 27. 33) Molecular Weight of [HNO] is 31. 34) Molecular Weight of [N] is 14. 35) Molecular Weight of [NNH] is 29. 36) Molecular Weight of [N2O] is 44. 37) Molecular Weight of [NH] is 15. 38) Molecular Weight of [NH2] is 16. 39) Molecular Weight of [NH3] is 17. 40) Molecular Weight of [NO] is 30. 41) Molecular Weight of [NO2] is 46. 42) Molecular Weight of [HCNO] is 43. 43) Molecular Weight of [HOCN] is 43. 44) Molecular Weight of [HNCO] is 43. 45) Molecular Weight of [NCO] is 42. 46) Molecular Weight of [CN] is 26. 47) Molecular Weight of [HCNN] is 41. 48) Molecular Weight of [N2] is 28. 49) Molecular Weight of [AR] is 40. 50) Molecular Weight of [C3H8] is 44. 51) Molecular Weight of [C3H7] is 43. 52) Molecular Weight of [CH3CHO] is 44. 53) Molecular Weight of [CH2CHO] is 43. >>
WORK SPACE


SAVED FOLDERS

PLOT FOR O2



PLOT FOR N2



PLOT FOR CO2



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.