Parsing NASA thermodynamic data using MATLAB
Parsing NASA thermodynamic data using MATLAB
(Parsing is reading and writing the data into the file to use it.)
OBJECTIVES:
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(Tip: use fopen and fgetl). The formulae are shown below.
Here R is the universal gas constant, T is the temeprature
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.
MAIN CODE with explanation:
close all
clc
% universal gas constant
R = 8.314 ;
%Reading header
f1 = fopen(\'THERMO.dat\',\'r\');
Header= fgetl(f1);
%Reading Global Temperature Line
Global_temp = fgetl(f1);
C = strsplit(Global_temp,\' \');
global_low_temp =str2double(C{2}); %Global Low Temperature
global_mid_temp =str2double(C{3}); %Global Mid Temperature
global_high_temp =str2double(C{4}); %Global High Temperature
fgetl(f1);
fgetl(f1);
fgetl(f1);
%using for loop to consider 53 elements
for i = 1:53
line= fgetl(f1); %Reading the line
A=strsplit(line,\' \'); %Seperating Variable
species=A(1); %Naming First Variable as \'Species\'
Q = strfind(line,\'G\');
local_low_temp = line(Q+3:55);
local_high_temp = line(Q+13:65);
local_mid_temp = line(Q+23:75);
lt = str2double(local_low_temp); %Local Low Temperature
ht = str2double(local_high_temp); %Local Mid Temperature
mt = str2double(local_mid_temp); %Local High Temperature
%line 1-Reading the coefficient data
cfline1= fgetl(f1);
X = strfind(cfline1,\'E\');
a1 = cfline1(1:X(1)+3);
a1 = str2double(a1); %storing 1st coefficient
a2 = cfline1(X(1)+4:X(2)+3);
a2 = str2double(a2); %storing 2nd coefficient
a3 = cfline1(X(2)+4:X(3)+3);
a3 = str2double(a3); %storing 3rd coefficient
a4 = cfline1(X(3)+4:X(4)+3);
a4 = str2double(a4); %storing 4th coefficient
a5 = cfline1(X(4)+4:X(5)+3);
a5 = str2double(a5); %storing 5th coefficient
%line 2-Reading coefficient data of 2nd line
cfline2 = fgetl(f1);
G = strfind(cfline2,\'E\');
a6 = cfline2(1:G(1)+3);
a6 = str2double(a6); %storing 6th coefficient
a7 = cfline2(G(1)+4:G(2)+3);
a7 = str2double(a7); %storing 7th coefficient
a8 = cfline2(G(2)+4:G(3)+3);
a8 = str2double(a8); %storing 8th coefficient
a9 = cfline2(G(3)+4:G(4)+3);
a9 = str2double(a9); %storing 9th coefficient
a10 = cfline2(G(4)+4:G(5)+3);
a10 = str2double(a10); %storing 10th coefficient
%line 3-Reading coefficient data of 3rd line
cfline3 = fgetl(f1);
I = strfind(cfline3,\'E\');
a11 = cfline3(1:I(1)+3);
a11 = str2double(a11); %storing 11th coefficient
a12 = cfline3(I(1)+4:I(2)+3);
a12 = str2double(a12); %storing 12th coefficient
a13 = cfline3(I(2)+4:I(3)+3);
a13 = str2double(a13); %storing 13th coefficient
a14 = cfline3(I(3)+4:I(4)+3);
a14=str2double(a14); %storing 14th coefficient
%Ranging the temperature low temperature to high temperature
T=linspace(lt,ht,500);
%Specific Heat
Cp=specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt);
%Entropy
S=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt);
%Enthaphy
H=enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt);
%Saving as a folder
spec=char(species);
fd = sprintf(\'%s\',spec);
cur_dir=pwd;
mkdir(spec);
cd(spec);
%Plot 1, Specific heat (Cp) vs Temperature (T)
figure(1);
plot(T,Cp,\'linewidth\',2,\'color\',\'r\');
xlabel(\'Temperature in K\');
ylabel(\'Specific heat in kJ\');
title(\'Specific heat vs Temperature\');
%Plot 2, Enthalpy (H) vs Temperature (T)
figure(2);
plot(T,H,\'linewidth\',2,\'color\',\'b\');
xlabel(\'Temperature in K\');
ylabel(\'Enthalpy in kJ/(mol)\');
title(\'Enthalpy vs Temperature\');
%Plot 3, Entropy (S) vs Temperature (T)
figure(3);
plot(T,S,\'linewidth\',2,\'color\',\'g\');
xlabel(\'Temperature in K\');
ylabel(\'Entropy in kJ/(mol K)\');
title(\'Entropy vs Temperature\');
% saving the plots that got generated
saveas(figure(1),\'cp.png\');
saveas(figure(2),\'h.png\');
saveas(figure(3),\'s.png\');
cd(cur_dir);
%Molecular mass
M= molecular_mass(species)
f2 = fopen(\'molecular mass\',\'w\');
fprintf(\'%s n %s n %f n\',\'species\',\'Molecular mass\',M)
fclose(f2)
end
fclose(f1);
fclose(f1);
1.to read the file fopen fgetl commands is used.
2.For calculating the coefficients strsplit command is used and to convert it str2num command is used.
3.Similarly then the specific heat , Enthalpy, Entropy are calculated and plotted .
4.using for loop read the remaining 52 species lines.
5.find out the coefficients and convert them into numbers
6. for different values of temperature find out the cp,H,S using if-else command
7.save the file using pwd command
FUNCTION CODES
1.ENTHALPY FUCTION
function H=enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt)
if T>=mt
H=(a1.*T+a2.*(T.^2)./2+a3.*(T.^3)./3+a4.*(T.^4)./4+a5.*(T.^5)./5+a6)*R;
else
H=(a8.*T+a9.*(T.^2)./2+a10.*(T.^3)./3+a11.*(T.^4)./4+a12.*(T.^5)./5+a13)*R;
end
2.SPECIFIC HEAT FUNCTION
function Cp=specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt)
if T>=mt
Cp=(a1+a2.*(T)+a3.*(T.^2)+a4.*(T.^3)+a5.*(T.^4))*R;
else
Cp=(a8+a9.*(T)+a10.*(T.^2)+a11.*(T.^3)+a12.*(T.^4))*R;
end
3.ENTROPY FUNCTION
function S=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt)
if T>=mt
S=(a1.*log(T)+a2.*T+a3.*(T.^2)./2+a4.*(T.^3)./3+a5.*(T.^4)./4+a7)*R;
else
S=(a8.*log(T)+a9.*T+a10.*(T.^2)./2+a11.*(T.^3)./3+a12.*(T.^4)./4+a14)*R;
end
4.MOLECULAR MASS FUNCTION
%Function_for_Molecular_Weight%
function M= molecular_mass(species)
atoms={\'H\',\'C\',\'N\',\'O\',\'AR\'};
AtomicWeight=[1 12 14 16 40];
M=0;
for i=1:length(species)
for j=1:length(AtomicWeight)
if (strcmp(species(i),atoms(j))==1)
M=M+AtomicWeight(j);
g=j;
end
end
% Adding the weights of the repeating atoms
n=str2double(species(i));
if n>1
p=(n-1);
M=M+AtomicWeight(p)*(n-1);
end
end
end
ERRORS:
While coding i get an error as :
Error in fgetl (line 32)
[tline,lt] = fgets(fid);
Error in Data (line 10)
Header= fgetl(f1);
this occured due to note copying file in same directory as the file or due to data file get deleted mistakenly.
OUTPUT:
CO2:
N2:
O2:
LINKS:
folder link:
https://drive.google.com/open?id=1bWaTacaemfsWqlfNeJ2A_QPvEaehkFo9
https://drive.google.com/open?id=1AikCHDlz9s_fu8whE81qee_GD4AyYpCP