Parsing NASA thermodynamic data
Aim:
Write a programe to parse NASA\'s thermodynamic data file and calculates the enthalpy, entropy and specific heats for all the species in the data file.
Introduction:
Parsing:
Parsing is the process of splitting file into pieces of data that can be easily stored or manipulated.so we are spliting string into parts then recognizing the part to convert it into something simpler than string.
Specific Heat(C_p):
Specific heat is defined as amount of heat required to raise temperature by one degree celsius.
Enthalpy(H):
Enthalpy is the measure of total energy content of body.Enthalpy H can be written as,
H = U + pV
Where,
H = Enthalpy of the system
U = Internal energy of the system
p = Pressure of the system
V = Volume of the system
Enthalpy is not measured directly, however, the change in enthalpy (ΔH) is measured, which is the heat added or lost by the system.At constant temperature, for the process heat flow is equal to the change in enthalpy,
Entropy(S):
Entropy is defined as it is part of energy that can not be converted into useful mechanical work.Entropy generation is measure of magnitude of irreversibility present during that process.Entropy generation can be used as a quantative measure of irreversibility associated with process. Change in entropy can be calculated by
dS=dQ/T
Where
dS= change in entropy
dQ=Heat transfer
T=Temperature of system
Formulae of Specific Heat,Enthalpy& Entropy used for calculation
The first 7 co-efficients are High temperature co-efficients and next 7 co-efficients are Low temperature co-efficients.
Code:
Function for Specific Heat
function [C_p]=specific_heat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,R,temp,local_mid_temp);
if temp<=local_mid_temp
C_p=R.*((b1+(b2*temp)+(b3*temp).^2)+(b4*(temp).^3)+(b5*(temp).^4));
else
C_p=R.*(a1+(a2*temp)+(a3*(temp).^2)+(a4*(temp).^3)+(a5*(temp).^4));
end
end
Function for Enthalpy
function [H]=Enthalpy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,R,temp,local_mid_temp);
if temp<=local_mid_temp
H=R.*temp.*(b1+(b2.*(temp))+(b3.*(temp./3))+(b4.*(temp./4))+(b5.*(temp./5))+(b6./temp));
else
H=R.*temp.*(a1+(a2.*(temp./2))+(a3.*(temp./3))+(a4.*(temp./4))+(a5.*(temp./5))+(a6./temp));
end
end
Function for Entropy
function [S]=entropy(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,R,temp,local_mid_temp);
if temp<=local_mid_temp
S=R.*((b1.*log(temp))+(b2*temp)+(b3*((temp).^2)./2)+(b4*(((temp).^3)./3))+(b5*(((temp).^4)./4))+b7);
else
S=R.*((a1.*log(temp))+(a2.*temp)+(a3.*((temp).^2)./2)+(a4.*((temp).^3)./3)+(a5.*(((temp).^4)./4))+a7);
end
end
Function for Molecular weight
% Molecular weight
function [mw]= molecular_weight(species_name)
elements = [\'H\',\'C\',\'N\',\'O\',\'S\',\'AR\'];
atomic_weight=[1.007,12.011,14.006,15.999,32.064,39.948];
mw=0;
for i=1:length(species_name)
for j=1:length(elements)
if strcmp(species_name(i),elements(j));
mw=mw+atomic_weight(j);
a=j;
end
endfor
n=str2double(species_name(i));
if n>1
mw=mw+atomic_weight(a)*(n-1);
end
endfor
endfunction
Main Code:
clear all
close all
clc
f1=fopen(\'THERMO.dat\',\'r\');
line1=fgetl(f1);
line2=fgetl(f1);
t=strsplit(line2,\' \');
R=8.314;
global_low_temp=str2double(t(2));
global_high_temp=str2double(t(3));
global_mid_temp=str2double(t(4));
for i=1:3
fgetl(f1);
end
for i=1:53
line_1=fgetl(f1);
A=strsplit(line_1,\' \');
species_name=A{1}
B=strfind(line_1,\'G\');
local_low_temp=str2double(line_1(49:55));
local_high_temp=str2double(line_1(58:66));
local_mid_temp=str2double(line_1(68:75));
temp=linspace(local_low_temp,local_high_temp,300);
tline_2=fgetl(f1);
tline_3=fgetl(f1);
tline_4=fgetl(f1);
a=strfind(tline_2,\'E\');
b=strfind(tline_3,\'E\');
c=strfind(tline_4,\'E\');
%high temp coeff
a1=str2double(tline_2(1:a(1)+3));
a2=str2double(tline_2(a(1)+4:a(2)+3));
a3=str2double(tline_2(a(2)+4:a(3)+3));
a4=str2double(tline_2(a(3)+4:a(4)+3));
a5=str2double(tline_2(a(4)+4:a(5)+3));
a6=str2double(tline_3(1:b(1)+3));
a7=str2double(tline_3(b(1)+4:b(2)+3));
%low temp coeff
b1=str2double(tline_3(b(2)+4:b(3)+3));
b2=str2double(tline_3(b(3)+4:b(4)+3));
b3=str2double(tline_3(b(4)+4:b(5)+3));
b4=str2double(tline_4(1:c(1)+3));
b5=str2double(tline_4(c(1)+4:c(2)+3));
b6=str2double(tline_4(c(2)+4:c(3)+3));
b7=str2double(tline_4(c(3)+4:c(4)+3));
C_p=specific_heat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,R,temp,local_mid_temp)*R;
H=Enthalpy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,R,temp,local_mid_temp)*R.*temp;
S=entropy(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,R,temp,local_mid_temp)*R;
figure(1)
plot(temp,C_p,\'linewidth\',2,\'color\',\'r\')
title([\'T vs C_p:-\' species_name]);
xlabel(\'Temperature\');
ylabel(\'Specific heat\');
figure(2)
plot(temp,H,\'linewidth\',2,\'color\',\'b\')
title([\'T vs H:-\' species_name]);
xlabel(\'Temperature\');
ylabel(\'Enthalpy\');
figure(3)
plot(temp,S,\'linewidth\',2,\'color\',\'g\')
title([\'T vs S:-\' species_name]);
xlabel(\'Temperature\');
ylabel(\'Entropy\');
mkdir(species_name) %create folder species_name
directory=pwd; %pwd is used to know path of current directory
cd(species_name)
saveas(1,\'Specific_heat.jpg\')
saveas(2,\'Enthalpy.jpg\')
saveas(3,\'Entropy.jpg\')
cd(directory)
mw=molecular_weight(species_name)
disp([\'Molecular weight of:-\' species_name])
disp(mw)
end
fclose(f1) %close the file f1
Output plot for species:
1.Plots for O2
fig:Temperature vs Enthalpy
fig:Temperature vs Entropy
fig:Temperature vs Specific Heat
2.Plots for Co2
fig:Temperature vs Enthalpy
fig:Temperature vs Entropy
fig:Temperature vs Specific Heat
3.Plots for N2
fig:Temperature vs Enthalpy
fig:Temperature vs Entropy
fig:Temperature vs Specific Heat
Explanation of code:
1.fopen is use to open file \'THERMO.dat\' and \'r\' is readable format and save it as f1.
2.fgetl is used to read the each and every line of file.
3.line 2 contains global temperatures and present in in the form of string hence to extract the values of global temp split the string usig \'strsplit\' and save it as global low,mid or high temperatures.
3.for i=1:3
fgetl(f1);
end
it will read next 3 lines which are comment but since we use semicolon it will suppress the output within code and won\'t show those 3 comments.
4.There are total 53 molecule present in THERMO.dat file hence we use i=1:53
5.for every chemical element there are 4 lines, 1st element of 1st line is name of species and to extract name of species we used
A=strsplit(line_1,\' \');
species_name=A{1}
Curly brackets({}) are used since A is cell array.
6. Also line_1 contains values of local temperature different for different species
to extract it find the value of \'G\' using \'strfind\' and \'str2double\'converts string into double precise value.
7.Now to plot the smooth curve divide local temperature into 300 values starting from local low temperature to local high temperature using\'linspace\' command
Error:
1.Name of species is not shown in image
fig:Error
Steps taken to overcome error:
Use \'title([\'T vs S:-\' species_name])\' instead of \'title(\'species_name\')\'.
Workspace of programe:
fig:Workspace
Molecular weight of species shown in command window
fig: molecular weight of difeernt species
Plots of species saved in folder
fig: plots saved in folder
Refferences:
1.Parsing[https://en.wikipedia.org/wiki/Parsing]
2.Cell Array[https://in.mathworks.com/help/matlab/cell-arrays.html]
3.mkdir[https://in.mathworks.com/help/matlab/ref/mkdir.html]
4.skill-lync