NASA'S FILE PARSING THE THERMODYNAMIC DATA
AIM:
1. A function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file and
2. Calculate the molecular weight of each species and to plot the specific heat, enthalpy and entropy and local temperatures for each species.
THEORY:
FILE PARSING:
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.
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 perfoming some kind of transformation on the data.
WHAT IS SPECIFIC HEAT ? :
The specific heat is the amount of heat per unit mass required to raise the temperature by one degree Celsius. The relationship between heat and temperature change is usually expressed in the form shown below where c is the specific heat.
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
ATOMIC WEIGHT:
The total weight of an atom is called the atomic weight. It is approximately equal to the number of protons and neutrons, with a little extra added by the electrons. The stability of the nucleus, and hence the atom's radioactivity, is heavily dependent upon the number of neutrons it contains.
FORMULAS USED:
here,
CP = specific heat;
R = gas constant;
T = temperature;
H = enthalpy;
S = entropy;
COMMANDS USED:
- strsplit
- str2double
- mkdir
- disp
- fgetl
- strfind
MAIN CODE:
clear all
close all
clc
% to open the given file:
f1=fopen('THERMO.dat','r');
% to read the given every line:
tline_0=fgetl(f1);
tline_1=fgetl(f1);
% to split the string:
I=strsplit(tline_1,' ');
%changing string to number:
global_Low_temp=str2double(I{2});
global_mid_temp=str2double(I{3});
global_high_temp=str2double(I{4});
for i=1:3
HELLO=fgetl(f1)
end
for i=1:53
tline_2=fgetl(f1);
s = strsplit(tline_2,' ')
species_name=s{1}
A=strfind(tline_2,'G ');
temp_values=tline_2(A+4:length(tline_2));
B=strsplit(temp_values,' ');
local_low_temp=str2double(B{1});
local_high_temp=str2double(B{2});
local_mid_temp=str2double(B{3});
%to find the coefficients:
tline_3=fgetl(f1);
F=strfind(tline_3,'E');
a1=str2double(tline_3(1:(F(1)+3)));
a2=str2double(tline_3(F(1)+4:F(2)+3));
a3=str2double(tline_3(F(2)+4:F(3)+3));
a4=str2double(tline_3(F(3)+4:F(4)+3));
a5=str2double(tline_3(F(4)+4:F(5)+3));
tline_4=fgetl(f1);
F=strfind(tline_4,'E');
a6=str2double(tline_4(1:(F(1)+3)));
a7=str2double(tline_4(F(1)+4:F(2)+3));
a8=str2double(tline_4(F(2)+4:F(3)+3));
a9=str2double(tline_4(F(3)+4:F(4)+3));
a10=str2double(tline_4(F(4)+4:F(5)+3));
tline_5=fgetl(f1);
F=strfind(tline_5,'E');
a11=str2double(tline_5(1:(F(1)+3)));
a12=str2double(tline_5(F(1)+4:F(2)+3));
a13=str2double(tline_5(F(2)+4:F(3)+3));
a14=str2double(tline_5(F(3)+4:F(4)+3));
%assing gas constant values:
R=8.314;
T=linspace(local_low_temp,local_high_temp,100);
%calling specific heat function:
C_p = R*specific_heat_calc(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,local_low_temp,local_mid_temp,local_high_temp);
%calling enthalpy function:
s = R*entropy_calc(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,local_low_temp,local_mid_temp,local_high_temp);
%calling entropy function:
h = R*enthalpy_calc(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,local_low_temp,local_mid_temp,local_high_temp);
%finding required species:
req_species=["O2","N2","CO2"];
if ((species_name==req_species(1))||(species_name==req_species(2))||(species_name==req_species(3)))
mkdir (species_name);
cd(species_name)
%plotting by comparing for temperature and specific heat:
figure(1)
plot(T,C_p)
title('specific heat vs Temperature')
xlabel('Temperature (k)')
ylabel('specific heat (j/mol)')
image=getframe(gcf);
imwrite(image.cdata,'cpvsT.png')
%plotting by comparing for temperature and enthalpy:
figure(2)
plot(T,h)
title('enthalpy vs Temperature')
xlabel('Temperature (k)')
ylabel('enthalpy')
image=getframe(gcf);
imwrite(image.cdata,'hvsT.png')
%plotting by comparing for temperature and entropy:
figure(3)
plot(T,s)
title('entropy vs Temperature')
xlabel('Temperature (k)')
ylabel('entropy')
image=getframe(gcf);
imwrite(image.cdata,'SvsT.png')
cd ..
end
% to display the molecular weight:
Molecular_wt=molecular_weight(species_name);
k=sprintf('%s Mol_wt = %f',species_name,Molecular_wt);
disp(k)
end
EXPLANATION OF THE MAIN CODE:
NASA came up with polynomials that can be used to evaluate thermodynamic properties such as Cp, H and S. They have also documented the co-efficients that are required to evaluate these polynomials for 53 species.
- At first the given file is opened and the input/given data is to be read, by using dat. 'r'.
- the given 1st and 2nd line is readed and the split command is used to split the string into individuals.
- converting those 3datas to string to number i.e., string to number.
- in for loop, skiping the next 3 lines by reading (fgetl)
- then for loop is initialized for 53 species,
- then reding the next line and splitting thge line strsplit command.
- in that line to finding G as a common term so it is taken into account and splitted
- followed by local temperatures are converting string to number.
- then the coefficints are finded by finding the 'E' in every line to get the position
- after getting position, the individual values are finded out by reading every line.
- initializing the gas constant as R=8.314
- calling the function as defined earlier new tab as , specific heat , enthalpy, entropy and molecular weight.
- for sampling the three species are taken as O2,N2,CO2.
- by calling the every species name the calculations are done and given by function method.
- make directory is save the program.
- plotting the graph in figure(1) for temperature and specific heat.
- then,plotting the the graph in figure(2) for temperature and enthalpy and
- plotting the graph in figure(3) for entropy.
- by simulatenously giving the label for the each plot.
- and similarly the images are saved for each plotting by 'png' format.
- at last the calling the molecular function by calling method
- and the molecular weights are calucalted in the function and then the caluculated values are displayed in the command wind by using disp command.
MOLECULAR WEIGHT CALLING FUNCTION:
function[M]= molecular_weight(species_name)
Atomic_mass=[15.999,1.008,12.011,14.007,39.948];
Elements=['O','H','C','N','A'];
Mol_wt=0;
for i=1:length(species_name)
for j=1:length(Elements)
if strcmpi(species_name(i),Elements(j))
Mol_wt=Mol_wt+Atomic_mass(j);
z=j;
end
end
var=str2double(species_name(i));
if (var>1)
Mol_wt=Mol_wt+(Atomic_mass(z)*(var-1));
end
end
M=Mol_wt;
end
ENTHALPY CALLING FUNCTION:
function h = enthalpy_calc(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,Local_low_temp,Local_mid_temp,Local_high_temp)
if(T>Local_mid_temp)
h=(a1+(a2*(T.^2)/2)+(a3*(T.^3)/3)+(a4*(T.^4)/4)+(a5*(T.^5)/5)+a6);
else
h=(a8+(a9*(T.^2)/2)+(a10*(T.^3)/3)+(a11*(T.^4)/4)+(a12*(T.^5)/5)+a13);
end
here, the enthalpy is called for main program and the values are caluclated by the above given formulas by comparing the temperature constant and the enthalpy calculated values.
as the caluculated value is generated below are low local low temperature and above values are local high temperatures
ENTROPY CALLING FUNCTION:
function [S]=entropy_calc(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,Local_low_temp,Local_mid_temp,Local_high_temp)
if(T>Local_mid_temp)
S=(a1*log(T)+(a2*T)+(a3*(T.^2)/2)+(a4*(T.^3))+(a5*(T.^4)/4)+a7);
else
S=(a8*log(T)+(a9*T)+(a10*(T.^2)/2)+(a11*(T.^3))+(a12*(T.^4)/4)+a13);
end
here, the entropy is called for main program and the values are caluclated by the above given formulas by comparing the temperature constant and the entropy calculated values.
as the caluculated value is generated as below are low local low temperature and above values are local high temperatures.
SPECIFIC HEAT CALLING FUNCTION:
function [C_p]= specific_heat_calc(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,Local_low_temp,Local_mid_temp,Local_high_temp)
if(T>Local_mid_temp)
C_p=(a1+(a2*T)+(a3*(T.^2))+(a4*(T.^3))+(a5*(T.^4)));
else
C_p=(a8+(a9*T)+(a10*(T.^2))+(a11*T.^3)+(a12*T.^4));
end
here, the enthalpy is called for main program and the values are caluclated by the above given formulas by comparing the temperature constant and the specific heat calculated values.
as the caluculated value is generated below are low local low temperature and above values are local high temperatures.
RESULTS:
CO2:
N2:
02:
CONCLUSION:
Thus the function that extracts the 14 co-efficients and calculated the enthalpy, entropy and specific heats for all the species in the data file and as well as the molecular weight of each species are calculated and plotted done by the specific heat, enthalpy and entropy and local temperatures for each species.