Project 1 - Parsing NASA thermodynamic data
clear all
close all
clc
f1=fopen('THERMO.dat'); opening and saving in f1 variable
Filename=fgetl(f1); Filename Varibale - 1st fgetl will give the first line i.e. thermo
tline=fgetl(f1); 2nd fgetl will extract 2nd line in thermo.dat text file and will store in tline varibale
L=strsplit(tline,' '); strsplit will split 'tline' with 'spaces' and store in 'L'
L is having four string , 0 '' '300.000' '1000.000' '5000.000'
L(1) will ignored
global_low_temp=str2num(L{2}); L(2) is stroed in 'global_low_temp'. str2num-it converts string to number.
global_mid_temp=str2num(L{3}); L(3) is stroed in 'global_mid_temp'.
global_high_temp=str2num(L{4}); L(4) is stroed in 'global_high_temp'.
fgetl(f1); skipping next line
fgetl(f1); skipping next line
fgetl(f1); skipping next line
for i=1:53 initiating FOR loop upto 53 times
line_1=fgetl(f1); storing next line in "line_1'
spiecesname=line_1(1:6); storing 1 to 6 letter in 'spiecesname' variable example 'O'
t=strfind(line_1,'G'); storing the 'count of G (index of G) in "t"= t is 45
t1=line_1(t+4:t+6); storing letters from 45+5=49 , 45+6=51, means 49-51 letter
t2=line_1(t+13:t+16); similiarly t2, & t3
t3=line_1(t+23:t+26); we have t1= 300 , 5000, 1000
local_low_temp=str2double(t1); stored lower temperature t1 i.e. 300
local_high_temp=str2double(t2); stored High temperature t1 i.e. 5000
local_mid_temp=str2double(t3); stored mid temperature t1 i.e. 1000
X1=fgetl(f1); Extract next line in X1
Y1=strfind(X1,'E'); Storing index of E of line X1, in Y1 variable i.e 12 27 42 57 72
a1=str2double(X1(1:Y1(1)+3)); in X1, letter from 1 to 12+3, is stored in a1 , and str2double converts into number
a2=str2double(X1(Y1(1)+4:Y1(2)+3)); Similarly for a2
a3=str2double(X1(Y1(2)+4:Y1(3)+3)); Similarly for a3
a4=str2double(X1(Y1(3)+4:Y1(4)+3)); Similarly for a4
a5=str2double(X1(Y1(4)+4:Y1(5)+3)); Similarly for a5
Similarly extracting a6, a7 a8 ,a9 and a10 from next line
X2=fgetl(f1);
Y2=strfind(X2,'E');
a6=str2double(X2(1:Y2(1)+3));
a7=str2double(X2(Y2(1)+4:Y2(2)+3));
a8=str2double(X2(Y2(2)+4:Y2(3)+3));
a9=str2double(X2(Y2(3)+4:Y2(4)+3));
a10=str2double(X2(Y2(4)+4:Y2(5)+3));
Similarly extracting a11, a12, a13 a14 from next line
X3=fgetl(f1);
Y3=strfind(X3,'E');
a11=str2double(X3(1:Y3(1)+3));
a12=str2double(X3(Y3(1)+4:Y3(2)+3));
a13=str2double(X3(Y3(2)+4:Y3(3)+3));
a14=str2double(X3(Y3(3)+4:Y3(4)+3));
mkdir('D:\SKILL_LYNC\1. MATLAB\file parsing',spiecesname) mkdir=makedirectery a new folder with name 'spiecesname'
here we do calculate the SPECIFIC HEAT, ENTHALPY ,& ENTROPY
As said we using local temperature range, so we divide the range in 100 units, R is gas contant
T=linspace(local_low_temp,local_high_temp,100);
R=8.314;
[CP,H,S]=sp_properties(T,R,local_mid_temp,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14); it is function to solve CP, H & S
Main functiion is here:
function [CP,H,S]=sp_properties(T,R,local_mid_temp,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14)
for i=1:length(T)
if T(i)>local_mid_temp
from a1 to a7 is used because they are high temperature constant,
CP=(a1+(a2.*T)+(a3.*T.^2)+(a4.*T.^3)+(a5.*T.^4)).*R;
H=(a1+((a2.*T)./2)+((a3.*T.^2)./3)+((a4.*T.^3)./4)+((a5.*T.^4)./5)+(a6./T)).*T.*R;
S=(a1.*log(T)+(a2.*T)+((a3.*T.^2)./2)+((a4.*T.^3)./3)+((a5.*T.^4)./4)+a7).*R;
else From a7 to a14 is used because they are low temperature constant,
CP=(a8+(a9.*T)+(a10.*T.^2)+(a11.*T.^3)+(a12.*T.^4)).*R;
H=(a8+((a9.*T)./2)+((a10.*T.^2)./3)+((a11.*T.^3)./4)+((a12.*T.^4)./5)+(a13./T)).*T.*R;
S=(a8.*log(T)+(a9.*T)+((a10.*T.^2)./2)+((a11.*T.^3)./3)+((a12.*T.^4)./4)+a14).*R;
end
end
figure(1) here we plot btw T= temperature & CP=spec. heat
plot(T,CP,'linewidth',2,'color','y');
grid on
title(spiecesname)
xlabel('Temperature')
ylabel('Specific_Heat')
figure(2) here we plot btw T= temperature & H=Enthalpy
plot(T,H,'linewidth',2,'color','r');
grid on
title(spiecesname)
xlabel('Temperature')
ylabel('Enthalpy')
figure(3) here we plot btw T= temperature & Entropy
plot(T,S,'linewidth',2,'color','b');
grid on
title(spiecesname)
xlabel('Temperature')
ylabel('Entropy')
cd(spiecesname) entering into the current folder of 'spiecesname"
saveas(figure(1),'Specific heat.jpg') saving the figure 1
saveas(figure(2),'Enthalpy.jpg') saving the figure 2
saveas(figure(3),'Entropy.jpg') saving the figure 3
cd('D:\SKILL_LYNC\1. MATLAB\file parsing') now, coming back to main folder, where code is written
now Calculation of Atomic weight:
elements=['H', 'C', 'O', 'N','A','S']; in whole spieces name these 6 letter are available
atomic_weight=[1.00794, 12.0107, 15.999, 14.0067, 39.948, 32.00]; these are thier atomic weight
molecular_weight_calculation=0;
initial weight is Zero , so that for next run of for loop it will not take before value
for i=1:length(spiecesname) for every value of 'i' j will have 6 value of elements w.r.t thier atomic weight
for j=1:length(elements)
if strcmp(spiecesname(i),elements(j)) we are comparing our spieces name with element each value
molecular_weight_calculation=molecular_weight_calculation+atomic_weight(j); weight= 0+'element value that matches for 'O'=15.999'
a=j; a=j=3 if satisfied at O
end
end loops ends after comparing each value
n=str2double(spiecesname(i));
if here for O , no number are there so , skips. (for O2 , '2' is aplicable here and n will be 2
if n>1; since O is already taken one time before (n-1) means 2-1= 1so, below if O2, will be 16+16
molecular_weight_calculation=molecular_weight_calculation+atomic_weight(a)*(n-1)
end
end
end
PARSING NASA FILE PROGRAM FOR SPIECES
% PARSING NASA FILE
clear all
close all
clc
% EXTRACTING DATA FROM THERMO.DAT TEXT FILE
f1=fopen('THERMO.dat');
Filename=fgetl(f1);
tline=fgetl(f1);
L=strsplit(tline,' ');
global_low_temp=str2num(L{2});
global_mid_temp=str2num(L{3});
global_high_temp=str2num(L{4});
fgetl(f1);
fgetl(f1);
fgetl(f1);
% HERE WE ARE CALCULATING 53 NO OF SPIECES, THEIR CO-EFFICENT,
%THIER LOCAL TEMP RANGE
for i=1:53
line_1=fgetl(f1);
spiecesname=line_1(1:6);
t=strfind(line_1,'G');
t1=line_1(t+4:t+6);
t2=line_1(t+13:t+16);
t3=line_1(t+23:t+26);
local_low_temp=str2double(t1);
local_high_temp=str2double(t2);
local_mid_temp=str2double(t3);
X1=fgetl(f1);
Y1=strfind(X1,'E');
a1=str2double(X1(1:Y1(1)+3));
a2=str2double(X1(Y1(1)+4:Y1(2)+3));
a3=str2double(X1(Y1(2)+4:Y1(3)+3));
a4=str2double(X1(Y1(3)+4:Y1(4)+3));
a5=str2double(X1(Y1(4)+4:Y1(5)+3));
X2=fgetl(f1);
Y2=strfind(X2,'E');
a6=str2double(X2(1:Y2(1)+3));
a7=str2double(X2(Y2(1)+4:Y2(2)+3));
a8=str2double(X2(Y2(2)+4:Y2(3)+3));
a9=str2double(X2(Y2(3)+4:Y2(4)+3));
a10=str2double(X2(Y2(4)+4:Y2(5)+3));
X3=fgetl(f1);
Y3=strfind(X3,'E');
a11=str2double(X3(1:Y3(1)+3));
a12=str2double(X3(Y3(1)+4:Y3(2)+3));
a13=str2double(X3(Y3(2)+4:Y3(3)+3));
a14=str2double(X3(Y3(3)+4:Y3(4)+3));
% CREATING THE FOLDER IN CURRENT DIRECTRY,
mkdir('D:\SKILL_LYNC\1. MATLAB\file parsing',spiecesname)
T=linspace(local_low_temp,local_high_temp,100);
R=8.314;
[CP,H,S]=sp_properties(T,R,local_mid_temp,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14);
figure(1)
plot(T,CP,'linewidth',2,'color','y');
grid on
title(spiecesname)
xlabel('Temperature')
ylabel('Specific_Heat')
figure(2)
plot(T,H,'linewidth',2,'color','r');
grid on
title(spiecesname)
xlabel('Temperature')
ylabel('Enthalpy')
figure(3)
plot(T,S,'linewidth',2,'color','b');
grid on
title(spiecesname)
xlabel('Temperature')
ylabel('Entropy')
cd(spiecesname) % OPEING THE FOLDER AND SAVE ALL 3 FIGURES
saveas(figure(1),'Specific heat.jpg')
saveas(figure(2),'Enthalpy.jpg')
saveas(figure(3),'Entropy.jpg')
cd('D:\SKILL_LYNC\1. MATLAB\file parsing') % BACK TO MAIN FOLDER
% MOLECULAR WEIGHT CALCULATION
elements=['H', 'C', 'O', 'N','A','S'];
atomic_weight=[1.00794, 12.0107, 15.999, 14.0067, 39.948, 32.00];
molecular_weight_calculation=0;
for i=1:length(spiecesname)
for j=1:length(elements)
if strcmp(spiecesname(i),elements(j))
molecular_weight_calculation=molecular_weight_calculation+atomic_weight(j);
a=j;
end
end
n=str2double(spiecesname(i));
if n>1;
molecular_weight_calculation=molecular_weight_calculation+atomic_weight(a)*(n-1)
end
end
end
% THE END
fUNCTION PROGRAM
function [CP,H,S]=sp_properties(T,R,local_mid_temp,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14)
for i=1:length(T)
if T(i)>local_mid_temp
CP=(a1+(a2.*T)+(a3.*T.^2)+(a4.*T.^3)+(a5.*T.^4)).*R;
H=(a1+((a2.*T)./2)+((a3.*T.^2)./3)+((a4.*T.^3)./4)+((a5.*T.^4)./5)+(a6./T)).*T.*R;
S=(a1.*log(T)+(a2.*T)+((a3.*T.^2)./2)+((a4.*T.^3)./3)+((a5.*T.^4)./4)+a7).*R;
else
CP=(a8+(a9.*T)+(a10.*T.^2)+(a11.*T.^3)+(a12.*T.^4)).*R;
H=(a8+((a9.*T)./2)+((a10.*T.^2)./3)+((a11.*T.^3)./4)+((a12.*T.^4)./5)+(a13./T)).*T.*R;
S=(a8.*log(T)+(a9.*T)+((a10.*T.^2)./2)+((a11.*T.^3)./3)+((a12.*T.^4)./4)+a14).*R;
end
end

O2 PLOTS:



N2 PLOTS:


CO2 PLOTS:



ERRORS SOME OF:

IF WE DO RUN PROGRAM AGAIN SO THIS ERROR WILL COME SINCE THE FOLDER ALREADY CREATED IN FIRST ATTEMPT.