Project 1 - Parsing NASA thermodynamic data
Parsing- Nasa thermodynamic data file using Matlab
Aim:-
To write a function to extract the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the given NASA thermodynamic data file, and aslo to plot the respective graphs and to calculate the values of cp,H,S& molecular weight for each of the species. And also to make a seperate directory to put-up all the datas extracted.
Software used:-
MATLAB R2022a
Project Description:-
File parsing:-
The formulas to find out the cp.H,S are gives as follows:-

Here,
T= Local temperature
b1 to b7 = High temperature coefficients
b8 to b14= Low temperatute coefficients
R= Gas constant, and the value for R is 8.314
Now we will be provided with the following thermmodynamic data file:

Now our task is to extract the datas from these file and to find out the cp,H,S & molecular weight for each and every species mentioned in the data file.
PROGRAM TO READ THE THERMODYNAMIC DATA FILE & ITS EXPLAINATIONS:-
The 'str2num' code is used to convert the string values into numbers and they are stored in the seperate variables as global low,mid & high temperatures.
The lines starts with exclamatory symbol are comments and we can eliminate that. And hence the above 'for loop' is used to skip the next three lines.
And this line will be splitted with the help of the above strsplit code. And then the important task is to get the local low, mid and high temperatures and to convert them as numbers and hence it can be used for calculation purposes.
2.92175791E+04 4.78433864E+00 3.16826710E+00 -3.27931884E-03 6.64306396E-06 3
-6.12806624E-09 2.11265971E-12 2.91222592E+04 2.05193346E+00 4
Now to seperate the coefficient values, we are going to find out the position of "E' and hence the rest of the values can be easily extracted.














Molecular weight calculated values :-



MAIN PROGRAM:-
clear all
close all
clc
f1=fopen('THERMO.dat','r');
%To load the first two lines in the data file
first_line=fgetl(f1);
second_line_temp=fgetl(f1);
%To split the global temperature values with the help of space delimiter
global_temp=strsplit(second_line_temp,' ');
%To convert the string values as numbers
global_low_temp=str2num(global_temp{2});
global_mid_temp=str2num(global_temp{3});
global_high_temp=str2num(global_temp{4});
% To skip the comment lines in between
for i=1:3
fgetl(f1)
end
% To get the first data line consists of temperature for 'o'
for i=1:53
line1=fgetl(f1)
% To split the line
line2=strsplit(line1,' ');
%To get the low, mid & high temp seperately
sp_name=line2{1};
tl=line2{end-3};
th=line2{end-2};
tm=line2{end-1};
%To convert the string values into a number
tl=str2num(tl);
th=str2num(th);
tm=str2num(tm);
%To read the line consists of coefficient values
data=fgetl(f1);
position_of_e=findstr(data,'E');
c1=str2double(data(1:position_of_e(1)+3))
c2=str2double(data(position_of_e(1)+4:position_of_e(2)+3));
c3=str2double(data(position_of_e(2)+4:position_of_e(3)+3));
c4=str2double(data(position_of_e(3)+4:position_of_e(4)+3));
c5=str2double(data(position_of_e(4)+4:position_of_e(5)+3));
data2=fgetl(f1);
position_of_e=findstr(data2,'E');
c6=str2double(data2(1:position_of_e(1)+3));
c7=str2double(data2(position_of_e(1)+4:position_of_e(2)+3));
c8=str2double(data2(position_of_e(2)+4:position_of_e(3)+3));
c9=str2double(data2(position_of_e(3)+4:position_of_e(4)+3));
c10=str2double(data2(position_of_e(4)+4:position_of_e(5)+3));
data3=fgetl(f1);
position_of_e=findstr(data3,'E');
c11=str2double(data3(1:position_of_e(1)+3));
c12=str2double(data3(position_of_e(1)+4:position_of_e(2)+3));
c13=str2double(data3(position_of_e(2)+4:position_of_e(3)+3));
c14=str2double(data3(position_of_e(3)+4:position_of_e(4)+3));
R=8.314;
t=linspace(tl,th,100);
cp=specific_heat(c1,c2,c3,c4,c5,c8,c9,c10,c11,c12,th,t,R);
h=enthalpy(c1,c2,c3,c4,c5,c6,c8,c9,c10,c11,c12,c13,th,t,R);
s=entropy(c1,c2,c3,c4,c5,c7,c8,c9,c10,c11,c12,c14,th,t,R);
%molecular_weight=molecular_weight(sp_name);
molecular_weight_calc=molecular_weight(sp_name);
%To make the folder directory
cd('C:\Users\hp\OneDrive\Documents\MATLAB\Nasa File Parsing')
mkdir(sp_name)
cd(sp_name)
%Plotting
figure(1)
plot(t,cp,LineWidth=3,Color='r')
xlabel('Temperature')
ylabel('cp')
title(sp_name)
saveas(figure(1),'t vs cp','jpg')
figure(2)
plot(t,h,LineWidth=3,Color='g')
xlabel('Temperature')
ylabel('Enthalpy')
title(sp_name)
saveas(figure(2),'Enthalpy vs t','jpg')
figure(3)
plot(t,h,LineWidth=3,Color='b')
xlabel('Temperature')
ylabel('Entropy')
title(sp_name)
saveas(figure(3),'Entropy vs t','jpg')
cd('C:\Users\hp\OneDrive\Documents\MATLAB\Nasa File Parsing')
end
FUNCTION PROGRAM FOR Cp:-
%For specific heat
function [cp]=specific_heat(c1,c2,c3,c4,c5,c8,c9,c10,c11,c12,th,t,R)
if(t<=th)
cp=R*(c8+c9*t+c10*t.^2+c11*t.^3+c12*t.^4);
else
cp=R*(c1+c2*t+c3*t.^2+c4*t.^3+c5*t.^4);
end
end
FUNCTION PROGRAM FOR H (Enthalpy ):-
%For enthalpy
function [H]=enthalpy(c1,c2,c3,c4,c5,c6,c8,c9,c10,c11,c12,c13,th,t,R)
if(t<=th)
H=((c8+(c9.*t)/2)+((c10.*t.^2)/3)+((c11.*t.^3)/4)+((c12.*t.^4)/5)+(c13./t)).*t*R;
else
H=((c1+(c2.*t)/2)+((c3.*t.^2)/3)+((c4.*t.^3)/4)+((c5.*t.^4)/5)+(c6./t)).*t*R;
end
end
%For entropy
function [S]=entropy(c1,c2,c3,c4,c5,c7,c8,c9,c10,c11,c12,c14,th,t,R)
if(t<=th)
S=((c8.*log(t))+(c9.*t)+((c10.*(t.^2))/2)+((c11.*(t.^3)/3))+((c12.*(t.^4))/4)+c14)*R
else
S=((c1.*log(t))+(c2.*t)+((c3.*(t.^2))/2)+((c4.*(t.^3)/3))+((c5.*(t.^4))/4)+c7)*R
end
end
%For molecular weight
function mw=molecular_weight(sp_name)
species=(sp_name);
elements=['H' 'C' 'O' 'N' 'A'];
atomic_weight=[1 12 16 14 39];
mw=0;
for i=1:length(species);
for j=1:length(elements);
if strcmp(species(i),elements(j));
mw=mw+atomic_weight(j);
position=j;
end
end
n=str2num(species(i));
if n>1
mw=mw+(atomic_weight(position)*(n-1));
end
end
fprintf('Molecular weight of %s = %d',species,mw)
end

