File Parsing
Introduction
Through research and experimentation NASA has proposed functions for specific heat at constant presuure(Cp) , enthalapy(H) and entropy(S) which can be defined by knowing 14 coefficients that define the function . Term.dat file provided by NASA is used to calculate these thermodynamic properties .
Aim
Aim of the project is to read the therm.dat file which is in chemkin format , and use those values to plot variation of various thermodynamic properties with temperature , calculate their molecular mass ,and store the plot in specific directories with name as specie name .
Inputs
The only input is the therm.dat file .
Output
Output contains directories representing each of the species in therm.dat file and contain plots of various themodynamic properties named and saved only by running the code .
Temperatures
Global min ,mid and max temperature in the header of the file are extracted by using strsplit to split the line containing the three temperatures and white space is used to split them and str2num is used to convert string to floating point number .
Specie Name
fgetl is used to read the line containing the name of the specie and strsplit is used to split all the words before first white space to get the specie name.
Extracting Coefficients
Each coefficient is extracted using their position relative to each \'E\' in line .findstr command is used to find \'E\' and as each coefficient has fixed decimal places so relative position of starting and ending digit can be found and then str2num is used to convert it to a floating point number.Embeded for loops are used to find other coefficients and for other species in similar way .
New directory and saving
mkdir command is used to make new directory of the name same as that of specie and cd command is used to browse between different directories. saveas command is used to save the generated plots.
Molecular Mass
strcmp is used to compare various members of the name of specie and the Global set of elements. Logic used is that if any string member in specie name matches with the elements , its coreesponding mass is added to the molecular mass , which is initially set to 0 ,and c is count of number of times the same element appears.The molecular mass is written in a text file in the directory of the corresponding specie.
Code
Following is the matlab code formulated for this project
clear all
close all
clc
f1 = fopen(\'THERMO.dat\',\'r\')
%skipping the header
file_header=fgetl(f1);
%reading the Temperatures
Temp = fscanf(f1,\'%f\' ,[1 3]);
low_temp = Temp(1);
mid_temp = Temp(2);
high_temp = Temp(3);
%leave comment lines
comment_1 = fgetl(f1);
comment_2 = fgetl(f1);
comment_3 =fgetl(f1);
comment_4 = fgetl(f1);
for i=1:53
specie_name_line = fgetl(f1);
space = strfind(specie_name_line,\' \');
specie = specie_name_line(1:space(1)-1);
%reding coeffs
tline1 = fgetl(f1);
b = strfind(tline1,\'E\');
a(1:14)=0;
a=a\';
a(1) = str2num(tline1(1:b(1)+3));
for j=2:5
a(j) = str2num(tline1(b(j-1)+4:b(j)+3));
end
tline2 = fgetl(f1);
b = strfind(tline2,\'E\');
a(6) = str2num(tline2(1:b(1)+3));
for k=7:10
j=k-5;
a(k) = str2num(tline2(b(j-1)+4:b(j)+3));
end
tline3 = fgetl(f1);
b = strfind(tline3,\'E\');
a(11) = str2num(tline3(1:b(1)+3));
for l=12:14
j=l-10;
a(l) = str2num(tline3(b(j-1)+4:b(j)+3));
end
R= 8.314;
folder_name = specie;
mkdir(folder_name);
T = linspace(1,5000,50);
for j=1:length(T)
if T(j)< mid_temp
Cp = R*(a(1)+a(2).*T+a(3).*T.^2 + a(4).*T.^3+a(5).*T.^4);
H = R.*T.*(a(1)+(a(2).*T)./2+(a(3).*T.^2)/3+(a(4).*T.^3)/4+(a(5).*T.^4)/5+(a(6)./T));
S = R.*(a(1)*log(T)+(a(2).*T)+(a(3).*T.^2)/2+(a(4).*T.^3)/3+(a(5).*T.^4)/4+(a(7)));
else
Cp = R.*(a(8)+a(9).*T+a(10).*T.^2+a(11).*T.^3+a(12).*T.^4);
H = R.*T.*(a(8)+(a(9).*T)/2+(a(10).*T.^2)/3+(a(11).*T.^3)/4+(a(12).*T.^4)/5+(a(13)./T));
S = R.*(a(8).*log(T)+(a(9).*T)+(a(10).*T.^2)/2+(a(11).*T.^3)/3+(a(12).*T.^4)/4+(a(14)));
end
end
%plotting Graphs
figure(1);
title(specie);
plot(T,Cp);
xlabel(\'T\');
ylabel(\'Cp (specific heat at constant pressure)\');
figure(2);
title(specie);
plot(T,H);
xlabel(\'T\');
ylabel(\'H (enthalapy)\');
figure(3);
title(specie);
plot(T,S);
xlabel(\'T\');
ylabel(\'S (entropy)\');
molecular_mass = mol_mass(specie);
cd (specie);
saveas(1,\'Cp vs T.jpg\');
saveas(2,\'H vs T.jpg\');
saveas(3,\'S vs T.jpg\');
f3 = fopen(\'mmass\' , \'w\');
fprintf(f3 , \'molecular mass of \');
fprintf(f3 ,specie);
fprintf(f3 , \' = \');
fprintf(f3 , num2str(molecular_mass));
fclose(f3);
cd ..;
disp(specie)
disp(molecular_mass)
end
function [M_W] = mol_mass(specie)
R_elem= {\'H\',\'C\',\'N\',\'O\',\'A\'};
A_W = [1 12 14 16 40];
M_W = 0;
for i = 1:length(specie)
for j = 1:length(R_elem)
if strcmp(specie(i),R_elem(j))
M_W = M_W+A_W(j);
c = j;
end
end
n = str2num(specie(i));
if n>1
M_W = M_W+A_W(c)*(n-1);
end
end
Results
Following are resulting plots for O2 ,N2 and CO2
O2
following plot shows variation of Cp with temperature
following is the plot for enthalapy with temperature
following is the plot of entropy(S) with Temperature
N2
following is the plot for Cp vs T
following is for H vs T
following is for S vs T
CO2
following is Cp vs T for CO2
following is curve showing H vs T for CO2
following shows the curve for S vs T for CO2