Project 1 - Parsing NASA thermodynamic data
Program for file parsing
%Program for file parsing
close all
clear all
clc
%Reading file data
fid = fopen('THERMO.dat','r');
fgetl(fid);
t1=fgetl(fid);
%Extracting Global Temperature
Global_Temp = strsplit(t1,' ');
Global_Temp_low = str2num(Global_Temp{2});
Global_Temp_mid = str2num(Global_Temp{3});
Global_Temp_high = str2num(Global_Temp{4});
%Finding species in data
prompt = ' Enter Species Name = ';
Species = input( prompt,'s')
while ~ feof(fid)
s = fgetl(fid);
a = strsplit(s,' ');
b = a(1);
match = strcmpi(b , Species);
if match == 1
disp('Species Data Available')
%Extracting coefficients from data
Line_1 = fgetl(fid);
S_1 = findstr(Line_1,'E');
Coefficient_1 = Line_1(1:S_1(1)+3);
Coefficient_2 = Line_1(S_1(1)+4:S_1(2)+3);
Coefficient_3 = Line_1(S_1(2)+4:S_1(3)+3);
Coefficient_4 = Line_1(S_1(3)+4:S_1(4)+3);
Coefficient_5 = Line_1(S_1(4)+4:S_1(5)+3);
a1 = str2num(Coefficient_1);
a2 = str2num(Coefficient_2);
a3 = str2num(Coefficient_3);
a4 = str2num(Coefficient_4);
a5 = str2num(Coefficient_5);
Line_2 = fgetl(fid);
S_2 = findstr(Line_2,'E');
Coefficient_6 = Line_2(1:S_2(1)+3);
Coefficient_7 = Line_2(S_2(1)+4:S_2(2)+3);
Coefficient_8 = Line_2(S_2(2)+4:S_2(3)+3);
Coefficient_9 = Line_2(S_2(3)+4:S_2(4)+3);
Coefficient_10 = Line_2(S_2(4)+4:S_2(5)+3);
a6 = str2num(Coefficient_6);
a7 = str2num(Coefficient_7);
a8 = str2num(Coefficient_8);
a9 = str2num(Coefficient_9);
a10 = str2num(Coefficient_10);
Line_3 =fgetl(fid);
S_3 = findstr(Line_3,'E');
Coefficient_11 = Line_3(1:S_3(1)+3);
Coefficient_12= Line_3(S_3(1)+4:S_3(2)+3);
Coefficient_13 = Line_3(S_3(2)+4:S_3(3)+3);
Coefficient_14 = Line_3(S_3(3)+4:S_3(4)+3);
a11 = str2num(Coefficient_11);
a12 = str2num(Coefficient_12);
a13 = str2num(Coefficient_13);
a14 = str2num(Coefficient_14);
%Extracting the local temperature range of the input molecule
Local_low_temp_s = a(end-3);
Local_high_temp_s= a(end-2);
Local_mid_temp_s = a(end-1);
Local_low_temp = str2double(Local_low_temp_s);
Local_high_temp = str2double(Local_high_temp_s);
Local_mid_temp = str2double(Local_mid_temp_s);
break
elseif match == 0
disp('Species Data Not Available')
else
continue
end
end
f = upper(Species);
%Displaying Molecular Weight of the input molecule.
moleWeight = MW(Species);
disp('Molecule Entered')
disp(f)
disp('Molecular Weight')
disp(moleWeight)
Temp_Range = linspace(100,10000,9000);
R = 8.314;
%Obtaining value of SPECIFIC HEAT
cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,Local_mid_temp,Temp_Range,R);
%Obtaining value of ENTHALPY
h = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,Local_mid_temp,Temp_Range,R);
%Obtaining value of ENTROPY
s = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,Local_mid_temp,Temp_Range,R);
%creating directory to store the plots
mkdir(f);
%Plotting Cp v T
figure(1)
plot(Temp_Range,cp,'linewidth',3,'color','r')
grid on
xlabel('Temperature')
ylabel('Specific Heat (J/K)')
title(f)
%Plotting S v T
figure(2)
plot(Temp_Range,s,'linewidth',3,'color','b')
grid on
xlabel('Temperature')
ylabel('Entropy (J/K)')
title(f)
%Plotting h v T
figure(3)
plot(Temp_Range,h,'linewidth',3,'color','g')
grid on
xlabel('Temperature')
ylabel('Enthalpy (J/kg)')
title(f)
%Defining current folder
cd(f)
%Saving the images to current folder in specified formats
saveas(figure(1),'Cp vs T.jpg')
saveas(figure(2),'S vs T.jpg')
saveas(figure(3),'h vs T.jpg')
%Defining current folder back as parent folder
cd('D:\MATLAB')
%Closing the open files
fclose('all');
%Creating funcion for MOlecular weight
function Mol_Weight = MW(Species)
Weight = 0;
% input as molecule name
% we have H C N O Ar
atoms = ['H','C','N','O','A'];
mass = [1,12,14,16,40];
%Since we have to compare each characters for mass, using 2 for loop
for i = 1:length(Species)
for j = 1:length(atoms)
if strcmpi(Species(i),atoms(j))
Weight = Weight + mass(j);
y = j;
end
end
%for more number of same atoms in the molecule
number = str2num(Species(i));
if (number>1)
% for extra molecule we use (number -1) so adding one more weight
Weight = Weight + (mass(y)*(number - 1));
end
end
Mol_Weight = Weight;
end
%Function for Specific Heat
function cp_val =
specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,Local_mid_temp,Temp_Range,R)
if ( Temp_Range > Local_mid_temp)
cp_val = ( a1+ (a2*Temp_Range) + (a3 *(Temp_Range.^2)) + (a4*(Temp_Range.^3)) + (a5 * (Temp_Range.^4)))*R;
else
cp_val = ( a8+ (a9*Temp_Range) + (a10 *(Temp_Range.^2)) + (a11*(Temp_Range.^3)) + (a12 * (Temp_Range.^4)))*R;
end
end
% Function for Enthalpy
function h_val = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,Local_mid_temp,Temp_Range,R)
if ( Temp_Range > Local_mid_temp)
h_val = ((a1 + ((a2 .* Temp_Range )./2) + ((a3.*( Temp_Range.^2))./3) +((a4 .*(Temp_Range.^3))./4) + ((a5 .* Temp_Range.^4))./5) + (a6./Temp_Range)).* (R.*Temp_Range);
else
h_val = ((a8+ ((a9 .* Temp_Range)./2) + ((a10.*(Temp_Range.^2))./3) +((a11 .*(Temp_Range.^3))./4) + ((a12 .*(Temp_Range.^4))./5) + (a13./Temp_Range))).* (R.*Temp_Range);
end
end
%function for entropy
function s_val = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,Local_mid_temp,Temp_Range,R)
if ( Temp_Range > Local_mid_temp)
s_val= (((a1*log(Temp_Range)) + (a2 *Temp_Range) + ((a3*(Temp_Range.^2))/2) + ((a4*(Temp_Range.^3)) /3)+((a5*Temp_Range.^4))/4)+(a7))*R;
else
s_val= (((a8*log(Temp_Range)) + (a9 *Temp_Range) + ((a10*(Temp_Range.^2))/2) + ((a11*(Temp_Range.^3)) /3) + ((a12*(Temp_Range.^4))/4) + (a14)))*R;
end
end
First we read file with the help of fopen command with reading function and to extract data from the given file we use fgetl command.
With the help of fgetl command we get the global temperatures line to extract the values of temperature we split that line in cell form with the help of strsplit command, and converted in number format with the help of str2num command.
Then take the input as species name from user to extract the coefficient of same species in string format.
Within the while loop we extracting each lines first cell to compare with the user input, if match =1 then the coefficients as well as local temperatures get extracted or else it shows species data is not available.
To calculate molecular weight we written a function MW, an array is define for atom and their respective mass, if match with species the respective mass is taken from array and molecular weight get calculated for multiple molecules we converted species name to number and multiply to mass of that molecule.
For specific heat, entropy and enthalpy we define the coefficient, local mid temperature and temperature range . By defining the temp range we separate the high temperature coefficient and low temperature coefficient and use accordingly within given polynomial.
Mkdir command is use to make new directory to save plot, with the above calculated properties we plot Cp vs T, H vr T and S vr T.
With the help of cd command we are displaying currant folder in order to return to original folder after we save plot and new plot is again save in same folder.
Outputs
For O2



For N2



For CO2




Referances
https://www.mathworks.com/help/matlab/ref/feof.html
https://www.mathworks.com/matlabcentral/answers/497882-parsing-text-files-for-beginners
https://www.thoughtco.com/definition-of-molecular-weight-605369