Project 1 - Parsing NASA thermodynamic data
Aim:
To Parse the Nasa Thermodynamic Data Using Matlab and to caluclate the properties of various gases.
Objective:
1.. Write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file. You will be reading this file NASA thermodynamic data
2. Calculate the molecular weight of each species and display it in the command window.
3. Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.
4. Save the plots as images with appropriate names and dump them in separate folders for each species with suitable name. All these should be generated automatically.
5. The main program that you will submit will just take the "THERMO.dat" file as an input and generate Cp, H and S plots.
6.Screenshot of the window where all the folders are saved.
7.Plot for O2,N2 and Co2 species to be show.
THE FORMULAE:-
Where
R = Universal Gas Constant
T = temperature
Cp = Specific Heat(K)
S = Entroy(KJ/mol-K)
H = Enthalpy(KJ/mol)
a(i)=Coefficients where, 1= i to 14
File Parsing
Parsing,syntax analysis,or syntactic analysis is the process of analyzing a string of symbols,either in natrual language,computer languages or data strutures,conforming to the rules of a formal grammar.The term parsing comes from latin pars(orationis).meaning part of speech.Within computational linguistics the term is used to refer to the formal analysis by a computer of asentance or other string of words into its constituent, resulting in a parse tree showing their syntactic relation to reach other,which may also contain semantic and other information.Some parsing algorithms may generate a parse forest or list of parse trees for a syntactically ambiguous input.
Parsing a file means reading in a data stream of some sort and building an in meamory model of the semantic content of that data.This aims at facilitating performing some kind of transformation on the data.
NASA Thermodynamic File:-
NASA thermodynamic file which is THERMO.dat file in which there are temperature range and 53 species are given in these data and each species contaions 14 coefficients and temperature ranges and we have to parse these 14 coefficients to find Specific heat.Entropy and Enthalpy using formule
SPECIFIC HEAT:-
The specific heat is the amount of heat per unit mass required to rise the temperature by one degree Celsius.
ENTROPY:-
Entropy,the measure of a system's thermal eneary per unit temperature that is unavailable for doing useful work.Because work is obtained from ordered molecular motion, the amount of enotropy is also a measure of the molecular disorder,or randomness of a system.
ENTHALPY:-
Enthalpy, a thermodynamic quantity equivalent to the total heat content of a system. It is equal to the internal energy of the system plus the product of pressure and volume.In a system contained so as to prevent mass transfer,for process at constant pressure,the heat absorberved or released equal to change in enthalpy.
This is the function to caluate the specific heat
%specific Heat calculation Function
function Cp = Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,midtemp)
%calculating apecific heat for higher and lower temperature respectively.
for i=1:length(T)
if T(i) <= midtemp
Cp(i) = R*(a8+ (a9*T(i)) + (a10*(T(i)).^2) + (a11*(T(i)).^3) + (a12*(T(i)).^4));
else
Cp(i) = R*(a1+ (a2*T(i)) + (a3*T(i)^2) + (a4*T(i)^3) + (a5*T(i)^4));
end
end
end
- Now we well find the Specific Heat by creating specific heat function and assigning its related formula coefficient from 14 coefficients,Gas constant,Temperature and mid temperature.
- Now we will create a condition using the "if" loop and make the temperature less than and equal to mid temperature to specify lower temperature coefficient in the formula.
- Then we will use the eske condition to specify coefficient of Higher temperature coefficient in the formula.
- Then end the loop and function to calculate the "specific Heat".
Function to Find the entropy
% Entropy Calculation Function
function S = Entropy (a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,tempmid)
%calculating entroy for higher and lower temperature respectively.
- Now we well find the Specific Heat by creating specific heat function and assigning its related formula coefficient from 14 coefficients,Gas constant,Temperature and mid temperature.
- Now we will create a condition using the "if" loop and make the temperature less than and equal to mid temperature to specify lower temperature coefficient in the formula.
- Then we will use the eske condition to specify coefficient of Higher temperature coefficient in the formula.
- Then end the loop and function to calculate the "specific Heat".
Function to Find the entropy
% Entropy Calculation Function
function S = Entropy (a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,tempmid)
%calculating entroy for higher and lower temperature respectively.
for i=1:length(T)
if (T(i) <= tempmid)
S(i) = R*(a8.*log(T(i)) + (a9*T(i)) + ((a10*(T(i)).^2)/2) + ((a11*(T(i)).^3)/3) + ((a12*(T(i)).^4)/4) + a14);
else
S(i) = R*(a1.*log(T(i)) + (a2*T(i)) + ((a3*(T(i)).^2)/2) + ((a4*(T(i)).^3)/3) + ((a5*(T(i)).^4)/4) + a7);
end
end
STEPS:-
- Now we will find the Entroy function and assigning its related formula coefficient from 14 coefficients,Gas constant,temperature and mid temperature.
- Now we will create a condition using the "if" loop and make the temperature less than and equal to mid temperature to specify lower temperature coeficient in the formula.
- Then we will use the esle condition to specify coefficient of higher temperature coefficient in the formula.
- Then end the loop and function to calculate the "Entropy".
Function to find Enthalpy
%Enthalpy calculation function
function H = Enthalpy (a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,tempmid)
%calculating enthaly for higher and lower temperature respectively
for i=1:length(T)
if(T(i) <= tempmid)
H(i) = R*T(i).*(a8 + ((a9*T(i))/2) + ((a10*(T(i)).^2)/3) + ((a11*(T(i)).^3)/4) + ((a12*(T(i)).^4)/5) + ((a13./T(i))));
else
H(i) = R*T(i).*(a1 + ((a2*T(i))/2) + ((a3*(T(i)).^2)/3) + ((a4*(T(i)).^3)/4) + ((a5*(T(i)).^4)/5) + ((a6./T(i))));
end
end
STEPS:-
- Now we will find Enthalpy function and assigning its related formula coefficient from 14 coefficients ,Gas constant,temperature and mid temperature.
- Now we will create a condition using the "if" loop and make the temperature less than and equal to mid temperature to specify lower temperature coeficient in the formula.
- Then we will use the esle condition to specify coefficient of higher temperature coefficient in the formula.
- Then end the loop and function to calculate the "Enthalpy".
FUNCTION TO FIND THE MOLECULAR WEIGHT OF THE SPECIES :-
% MOlecular weight Calculation Function
function MW = Molecular_weight(species)
% Defining Atomic Name of the species
atomic_name = ['H','C','O','N','Ar'];
%Hydrogen , Carbon , Oxygen, Nitrogen , Argon
%Defining Atomic masses of main species
atomic_weight = [1.008 12.011 15.999 14.007 39.948];
%Molecular weight starting range
MW = 0;
%Adding Atomic Number to obtain particular value
%Loop runs to the length of the species
for i = 1:length(species)
% Loop runs to the length of the atomic name
for j = 1:length(atomic_name)
% function to compare 2 strings
if strcmp(species(i), atomic_name(j))
MW = MW + atomic_weight(j);
position = j;
end
end
%finding more elements in the species and incrementing atomic weight of the species
n = str2num(species(i));
if n>1
MW = MW + atomic_weight(position)*(n-1);
end
end
fprintf('Molecular Weight of %s : ',species);
fprintf('%f',MW);
disp(' ');
end
STEPS:-
First, create the Molecular_weight function MW and assign it the apecies input.
Then define the main elements of all 53 types in the atomic_name array.
- "H", "C", "O", "N", "Ar".
Then, using the key elements above, create an "atomic_weight" array from any source, such as the internet. This is:
- 00812.011 15,999 14,007 39,948.
It then assigns the starting numerator range from MW to 0, creates a 'for' loop condition that runs to the length of the type, and creates another nested 'for' loop that runs to the length of the string atomic_name. increase.
Then create a condition using the 'if' condition and the 'strcmp' command to compare the string 'species' and 'atomic_name' to calculate the molecular weight. If the comparison reveals both strings to be equal, assign the molecular weight. Then use the str2num command to convert the string to a number and assign it. Then we run an 'if' loop condition to see if the species contains more than one element. If there is more than one element, i.e. n > 1, multiply the position by n-1 to increase the atomic weight. atomic_weight array.
Next, print the molecular weight with the fprintf command and display it with disp.
- Firstly we create an Molecular_weight function MW and assigning its apecies input to that.
- Then define the main element of all 53 species in an array atomic_name that are :-'H','C','O','N','Ar'.
- Then by taking the above main elements well create thereatomic_weight array from any sourse like internet that are :- 00812.011 15.999 14.007 39.948.
- Then we will assign molecular starting range from MW to zero and then create "for" loop condition which runs to the length of the species and an another nested "for" loop is created which runs to the length of the string atomic_name.
- Then we will create acondition using "if" condition and "strcmp" command to compare the species and atomic_name strings to calculate molecular masses if by comparision both string found same then we will assign its molecular weight.
- Now we will convert the string to numbers using "str2num" command and assign to then we run acondition of "if"loop to find whether the species contains more than one element and if there are more than 1 elements that is n>1 then we will increment the atomic weight by multiplication of position and n-1 to the atomic_weight array.
- Then we will print the molecular weight using fprintf command and display it using disp.
MAIN CODE:-
%Input
clear all
close all
clc
%Gas constant (J/(mol-K))
R = 8.314;
%reading date file
%Open and read THERMO.dat file
f1 = fopen('THERMO.dat','r')
get = fgetl(f1);
%Now reading Temperature from this file
temp = fgetl(f1);
%Spliting
s = strsplit(temp);
%Converting strings cells into numbers
templow = str2num(s{2});
tempmid = str2num(s{3});
temphigh = str2num(s{4});
%Temperature Range
%Skipping Comment lines
for i = 1:3
get = fgetl(f1);
end
%Reading the species
for j = 1:53
tline = fgetl(f1);
% Splitting cell into array
A = strsplit(tline,' ');
% To read the name of the species
species = (A{1});
% Converting strings cell into numbers
lowtemp = str2num(A{end-3});
hightemp = str2num(A{end-2});
midtemp = str2num(A{end-1});
T = linspace(lowtemp,hightemp,1000);
%Getting next line
tline1 = fgetl(f1);
%Finding the string E
a = strfind(tline1, 'E');
%Finding first 7 higher and second & lower coefficients
%Finding the Higher coefficients
a1 = tline1(1:a(1)+3);
a1 = str2num(a1);
a2 = tline1(a(1)+4:a(2)+3);
a2 = str2num(a2);
a3 = tline1(a(2)+4:a(3)+3);
a3 = str2num(a3);
a4 = tline1(a(3)+4:a(4)+3);
a4 = str2num(a4);
a5 = tline1(a(4)+4:a(5)+3);
a5 = str2num(a5);
tline2 = fgetl(f1);
b = strfind(tline2, 'E');
a6 = tline2(1:b(1)+3);
a6 = str2num(a6);
a7 = tline2(b(1)+4:b(2)+3);
a7 = str2num(a7);
% Finding lower coefficients
a8 = tline2(b(2)+4:b(3)+3);
a8 = str2num(a8);
a9 = tline2(b(3)+4:b(4)+3);
a9 = str2num(a9);
a10 = tline2(b(4)+4:b(5)+3);
a10 = str2num(a10);
tline3 = fgetl(f1);
c = strfind(tline3, 'E');
a11 = tline3(1:c(1)+3);
a11 = str2num(a11);
a12 = tline3(c(1)+4:c(2)+3);
a12 = str2num(a12);
a13 = tline3(c(2)+4:c(3)+3);
a13 = str2num(a13);
a14 = tline3(c(3)+4:c(4)+3);
a14 = str2num(a14);
% calculate specific heat
Cp = Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,tempmid);
% calculate Entropy
S = Entropy (a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,tempmid);
% calculate Enthalpy
H = Enthalpy (a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,tempmid);
%calculate Molicular weight of the species
MW = Molecular_weight(species);
%writing molecular weight file
MW_file = fopen('Molecular Weight.txt','w');
fprintf(MW_file,'Molecular weight of %s is %d ',species,MW);
%closing the file
mkdir(['D:\plots\',species])
cd(['D:\plots\',species])
%Plotting Cp, H & S with temperature
%plot 1| Cp vs T
figure(1)
plot(T,Cp,'linewidth',3,'color','r');
xlabel('Temperature (K)');
ylabel('Specific heat (KJ)');
title(sprintf('Specific heat vs Temperature range of %s',species));
grid on
saveas(1,'specific_heat.jpg');
%plot 2| S vs T
figure(2)
plot(T,S,'linewidth',3,'color','g');
xlabel('Temperature (K)');
ylabel('Entropy (KJ/mol-K)');
title(sprintf('Entropy vs Temperature range of %s',species));
grid on
saveas(2,'Entropy.jpg');
%plot 3| H vs T
figure(3)
plot(T,H,'linewidth',3,'color','b');
xlabel('Temperature (K)');
ylabel('Enthalpy (KJ/mol-K)');
title(sprintf('Enthalpy vs Temperature range of %s',species));
grid on
saveas(3,'Enthalpy.jpg');
%Molecular Weight
MW = Molecular_weight(species)
end
fclose(MW_file);
STEPS:-
- Clear all,close all and clc is used to clear all previous commands and plots
- fopen is used to open the file named 'dat'
- 'r' is used for read mode
- 'fgetl' is used to read the file line-by-line.
- 'strplit' command is used for splitting the string or character vectors at specified delimiter.
- 'str2num' command is used to convert character array or string to numeric array
- Using 'for & end' loops to skip the next 3 comments
- 'linspace' is used to generate the temperature range
- 'for' loop to read all 53 species one by one and plot them to different folder
- 'strfind' to find one string within another
- All function defined previously like the "specific Heat,Entropy,Enthalpy&Molecular weight" are called one by one.
- Calculate and display molecular weight
- 'fclose'is used to close the data file.
RESULTS AND PLOTS:-
PLOT :-
Plot for O2



PLOT FOR N2



PLOT FOR Co2




Molecular Weight of O : 15.999000
MW =
15.9990
Molecular Weight of O2 : 31.998000
Molecular Weight of O2 : 31.998000
MW =
31.9980
Molecular Weight of H : 1.008000
Molecular Weight of H : 1.008000
MW =
1.0080
Molecular Weight of H2 : 2.016000
Molecular Weight of H2 : 2.016000
MW =
2.0160
Molecular Weight of OH : 17.007000
Molecular Weight of OH : 17.007000
MW =
17.0070
Molecular Weight of H2O : 18.015000
Molecular Weight of H2O : 18.015000
MW =
18.0150
Molecular Weight of HO2 : 33.006000
Molecular Weight of HO2 : 33.006000
MW =
33.0060
Molecular Weight of H2O2 : 34.014000
Molecular Weight of H2O2 : 34.014000
MW =
34.0140
Molecular Weight of C : 12.011000
Molecular Weight of C : 12.011000
MW =
12.0110
Molecular Weight of CH : 13.019000
Molecular Weight of CH : 13.019000
MW =
13.0190
Molecular Weight of CH2 : 14.027000
Molecular Weight of CH2 : 14.027000
MW =
14.0270
Molecular Weight of CH2(S) : 14.027000
Molecular Weight of CH2(S) : 14.027000
MW =
14.0270
Molecular Weight of CH3 : 15.035000
Molecular Weight of CH3 : 15.035000
MW =
15.0350
Molecular Weight of CH4 : 16.043000
Molecular Weight of CH4 : 16.043000
MW =
16.0430
Molecular Weight of CO : 28.010000
Molecular Weight of CO : 28.010000
MW =
28.0100
Molecular Weight of CO2 : 44.009000
Molecular Weight of CO2 : 44.009000
MW =
44.0090
Molecular Weight of HCO : 29.018000
Molecular Weight of HCO : 29.018000
MW =
29.0180
Molecular Weight of CH2O : 30.026000
Molecular Weight of CH2O : 30.026000
MW =
30.0260
Molecular Weight of CH2OH : 31.034000
Molecular Weight of CH2OH : 31.034000
MW =
31.0340
Molecular Weight of CH3O : 31.034000
Molecular Weight of CH3O : 31.034000
MW =
31.0340
Molecular Weight of CH3OH : 32.042000
Molecular Weight of CH3OH : 32.042000
MW =
32.0420
Molecular Weight of C2H : 25.030000
Molecular Weight of C2H : 25.030000
MW =
25.0300
Molecular Weight of C2H2 : 26.038000
Molecular Weight of C2H2 : 26.038000
MW =
26.0380
Molecular Weight of C2H3 : 27.046000
Molecular Weight of C2H3 : 27.046000
MW =
27.0460
Molecular Weight of C2H4 : 28.054000
Molecular Weight of C2H4 : 28.054000
MW =
28.0540
Molecular Weight of C2H5 : 29.062000
Molecular Weight of C2H5 : 29.062000
MW =
29.0620
Molecular Weight of C2H6 : 30.070000
Molecular Weight of C2H6 : 30.070000
MW =
30.0700
Molecular Weight of CH2CO : 42.037000
Molecular Weight of CH2CO : 42.037000
MW =
42.0370
Molecular Weight of HCCO : 41.029000
Molecular Weight of HCCO : 41.029000
MW =
41.0290
Molecular Weight of HCCOH : 42.037000
Molecular Weight of HCCOH : 42.037000
MW =
42.0370
Molecular Weight of H2CN : 28.034000
Molecular Weight of H2CN : 28.034000
MW =
28.0340
Molecular Weight of HCN : 27.026000
Molecular Weight of HCN : 27.026000
MW =
27.0260
Molecular Weight of HNO : 31.014000
Molecular Weight of HNO : 31.014000
MW =
31.0140
Molecular Weight of N : 14.007000
Molecular Weight of N : 14.007000
MW =
14.0070
Molecular Weight of NNH : 29.022000
Molecular Weight of NNH : 29.022000
MW =
29.0220
Molecular Weight of N2O : 44.013000
Molecular Weight of N2O : 44.013000
MW =
44.0130
Molecular Weight of NH : 15.015000
Molecular Weight of NH : 15.015000
MW =
15.0150
Molecular Weight of NH2 : 16.023000
Molecular Weight of NH2 : 16.023000
MW =
16.0230
Molecular Weight of NH3 : 17.031000
Molecular Weight of NH3 : 17.031000
MW =
17.0310
Molecular Weight of NO : 30.006000
Molecular Weight of NO : 30.006000
MW =
30.0060
Molecular Weight of NO2 : 46.005000
Molecular Weight of NO2 : 46.005000
MW =
46.0050
Molecular Weight of HCNO : 43.025000
Molecular Weight of HCNO : 43.025000
MW =
43.0250
Molecular Weight of HOCN : 43.025000
Molecular Weight of HOCN : 43.025000
MW =
43.0250
Molecular Weight of HNCO : 43.025000
Molecular Weight of HNCO : 43.025000
MW =
43.0250
Molecular Weight of NCO : 42.017000
Molecular Weight of NCO : 42.017000
MW =
42.0170
Molecular Weight of CN : 26.018000
Molecular Weight of CN : 26.018000
MW =
26.0180
Molecular Weight of HCNN : 41.033000
Molecular Weight of HCNN : 41.033000
MW =
41.0330
Molecular Weight of N2 : 28.014000
Molecular Weight of N2 : 28.014000
MW =
28.0140
Molecular Weight of AR : 39.948000
Molecular Weight of AR : 39.948000
MW =
39.9480
Molecular Weight of C3H8 : 44.097000
Molecular Weight of C3H8 : 44.097000
MW =
44.0970
Molecular Weight of C3H7 : 43.089000
Molecular Weight of C3H7 : 43.089000
MW =
43.0890
Molecular Weight of CH3CHO : 44.053000
Molecular Weight of CH3CHO : 44.053000
MW =
44.0530
Molecular Weight of CH2CHO : 43.045000
Molecular Weight of CH2CHO : 43.045000
MW =
43.0450
Conclusion:
Using Octave code we have calculated the values of Nasa Thermodynamic data.For each species, the specific heat, entropy, and enthalpy are computed and plotted versus the local temperature range. For each species, the molecular weight is computed.