Project 1 - Parsing NASA thermodynamic data
AIM:
To write a code in Matlab/Octave to parse the NASA thermodynamic data file and then calculate thermodynamic properties of various gas species.
OBJECTIVE:
- 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.
- Calculate the molecular weight of each species and display it in the command window.
- Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.
- 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.
- The main program that you will submit will just take the "THERMO.dat" file as an input and generate Cp, H and S plots for all species that are included in the file.
THEORY:
Parsing is the process of analyzing text made of a sequence of tokens to determine its grammatical structure with respect to a given (more or less) formal grammar. The parser then builds a data structure based on the tokens. parsing is a compiler that is used to break the data into smaller elements coming from lexical analysis phase. A parser takes input in the form of sequence of tokens and produces output in the form of parse tree.
INPUT AND FORMULA:
- Universal Gas Constant
R= 8.3145
- Formula to calculates the enthalpy, entropy and specific heats for all the species in the data file
MAIN PROGRAM:
clear all
close all
clc
%reading the data file
f1=fopen('THERMO.dat','r');
%reading data file first line
header=fgetl(f1)
%global temperature
l1= fgetl(f1);
Temp=strsplit(l1,' ');
global_min=str2num(Temp{2})
global_mid=str2num(Temp{3})
global_max=str2num(Temp{4})
%not considering the next 3 lines
for i=1:3
l2=fgetl(f1);
end
%reading next line
l3=fgetl(f1);
%parsing
at=strfind(l3,'.');
t=strsplit(l3(at-3:end-1),' ');
%calculaing temp
local_min=str2num(t{1})
local_mid=str2num(t{2})
local_max=str2num(t{3})
T=linspace(local_min,local_max,200);
mkdir parsing_data
for j=1:53
species=strtok(l3,' ');
cd parsing_data
mkdir(species)
cd(species)
%co-efficient
a=fgetl(f1);
b=fgetl(f1);
c=fgetl(f1);
%finding position if E
d=strfind(a,'E');
e=strfind(b,'E');
f=strfind(c,'E');
%extracting high temperature coeff
a1=str2num(a(1:d(1)+3));
a2=str2num(a(d(1)+4:d(2)+3));
a3=str2num(a(d(2)+4:d(3)+3));
a4=str2num(a(d(3)+4:d(4)+3));
a5=str2num(a(d(4)+4:d(5)+3));
a6=str2num(b(1:e(1)+3));
a7=str2num(b(e(1)+4:e(2)+3));
%extracting low temperature coeff
b1=str2num(b(e(2)+4:e(3)+3));
b2=str2num(b(e(3)+4:e(4)+3));
b3=str2num(b(e(4)+4:e(5)+3));
b4=str2num(c(1:f(1)+3));
b5=str2num(c(f(1)+4:f(2)+3));
b6=str2num(c(f(2)+4:f(3)+3));
b7=str2num(c(f(3)+4:f(4)+3));
%universal gas constant
R = 8.3145;
%specific heat,entrpy,enthalpy
[cp S H]=specificheat(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,T,local_mid);
%plot
figure(1)
plot(T,cp,'color','r','linewidth',3);
xlabel('temperature(K)');
ylabel('specific heat(J/KG)');
figure(2)
plot(T,H,'color','r','linewidth',3);
xlabel('temperature(K)');
ylabel('entropy(J/KG)');
figure(3)
plot(T,S,'color','r','linewidth',3);
xlabel('temperature(K)');
ylabel('enthalpy(J/KG)');
%saving jpg data
saveas(figure(1),'specific heat vs temperature.jpg');
saveas(figure(2),'entropy vs temperature.jpg');
saveas(figure(3),'enthalpy vs temperature.jpg');
%saving fig data
saveas(figure(1),'specific heat vs temperature.fig');
saveas(figure(2),'entropy vs temperature.fig');
saveas(figure(3),'enthalpy vs temperature.fig');
cd('C:\Users\Dell\Desktop\abin')
mkdir(species)
cd(species)
l3=fgetl(f1);
% calculating molecular weight
elements=['H','O','C','N','S','Ar'];
atomic_weight=[1.007,15.999,12.010,14.006,32.059,39.948];
mw=0;
for i=1:length(species)
for j=1:length(elements)
if strcmp(species(i),elements(j))==1
mw=atomic_weight(j)+mw;
Z=atomic_weight(j);
end
end
n = str2num(species(i));
if n>1;
mw=Z*(n-1)+mw;
end
end
disp(['molecular_weight of:',species])
disp(mw)
cd('C:\Users\Dell\Desktop\abin')
end
CODE EXPLANATION:
- Matlab is better to start with clear all, close all, clc command.
CLEAR ALL: The statement is used to clear all the data that have stored in the workspace.
CLOSE ALL: The statement is used delete all figures and close the plots.
CLC : The statement clear the command window.
- Using fopen command in the matlab we can read the given 'THERMO.dat' file.
- Using fgetl command we are reading and extracting the first line in the data file and storing it in the variable called header.
- Global temperanture.
1.The next line in the file contain of global temperature range
2.Using fgetl command we are extracting the line in the data file and storing it in the variable.
3.Using strsplit we are splitting a space between the values.
- Using for loop statementwe are neglecting the next three line in the code.
- Using fgetl statement we are reading the next line and assigning it in the l3 variable.
- Using strfind command we are finding the value which has decimal point in it.
- Creating a new folder using mkdir command.
- Then we are creating loop for 53 specius to extract the coefficient
- Using strtok function is used to tokenize a string and thus separates it into multiple strings divided by a delimiter. All the next calls with the first argument being NULL use the string passed at the first call and return the next substring. The function returns NULL if no more substrings are available.
- Then using cd command we are changing the current working directory.
- Same mkdir and cd command is used in the next consecutive line for the species.
- Then after that using fgetl command we are finding the coefficient in the data file and storing it in variable a,b,c.
- Using strfind comment we are finding the position of E and storing it in the variable d,e,f.
- Using str2num we are converting character array or string to a numeric array same thing is followed in all the a consecutive steps in this first 7 are high temperature coefficient(a1,a2,a3,a4,a5,a6&a7) and the next 7 are low temprature coefficient (b1,b2,b3,b4,b5,b6,b7).
- In order to plot the graph first we have to calculate the enthalpy, entropy and specific heats for all the species in the data file. so we are creating a function.
- Where r=8.3145 which is universal gas constant.
Function program:
function[cp S H]=specificheat(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,T,local_mid);
if T<=local_mid
cp=R*(a1+(a2.*T)+(a3.*(T.^2))+(a4.*(T.^3))+(a5.*(T.^4)));
S=(a1+(a2*(T/2))+(a3*((T.^2)/.3))+(a4*((T.^3)/.4))+(a5*((T.^4)/.5))+(a6./T))*R.*T;
H=R*((a1.*log(T))+(a2.*T)+(a3.*((T.^2)./2))+(a4.*((T.^3)./3))+(a5.*((T.^4)./4))+a7);
else
cp=R*(b1+(b2.*T)+(b3.*(T.^2))+(b4.*(T.^3))+(b5.*(T.^4)));
S=(b1+(b2*(T/2))+(b3*((T.^2)/.3))+(b4*((T.^3)/.4))+(b5*((T.^4)/.5))+(b6./T))*R.*T;
H=R*((b1.*log(T))+(b2.*T)+(b3.*((T.^2)./2))+(b4.*((T.^3)./3))+(b5.*((T.^4)./4))+b7);
end
end
- Using the given formula we are finding the enthalpy, entropy and specific heats for all the species in the data file.
- Then we are creating the plots for enthalpy, entropy and specific heats for all the species vs temperature.
- Then we are saving the figures in savesa commnd in both jpg format and fig format.
- now using mkdir and cd a file can be created in the current directory to save the plots.
molecular weight:
elements=['H','O','C','N','S','Ar'];
atomic_weight=[1.007,15.999,12.010,14.006,32.059,39.948];
mw=0;
for i=1:length(species)
for j=1:length(elements)
if strcmp(species(i),elements(j))==1
mw=atomic_weight(j)+mw;
Z=atomic_weight(j);
end
end
n = str2num(species(i));
if n>1;
mw=Z*(n-1)+mw;
end
end
- To find the molecular weight we are using 6 elements ['H','O','C','N','S','Ar'] and their atomic weights [1.007,15.999,12.010,14.006,32.059,39.948].
- We are considering initial molecular weight as 0.
- Using for loop and string compare command we comparing the species with respect to string array.
- when the comparison becomes positive the program calculate the atomic weight to the old atomic weight of the current species.
- Then using the disp comment we are displaying the molecular weight of the species.
OUTPUT:
PLOTS :
O2:
Specific heat vs temperature plot:
entropy vs temperature plot:
enthalpy vs temperature plot:
N2:
Specific heat vs temperature plot:
entropy vs temperature plot:
enthalpy vs temperature plot:
CO2:
Specific heat vs temperature plot:
entropy vs temperature plot:
enthalpy vs temperature plot:
MOLECULAR WEIGHT OF SPECIES:
SCREEN SHOT WHERE THE SPECIES ARE SAVED:
RESULT:
Thus we have written a code in Matlab/Octave to parse the NASA thermodynamic data file and then calculate thermodynamic properties of various gas species.