parsing the information produced by nasa
1.
% MAIN PROGRAM FOR PARSING THE GIVEN DATA
clear all
close all
clc
file = fopen(\'THERMO.dat\',\'r\');
head1 = fgetl(file); % Reading 1st line
temp=fgetl(file); % Reading 2nd line global temps
split = strsplit(temp,\' \'); % Splitting the string into each cells
g_low = str2num(split{2}); % Conversion of string from a cell into a number
g_med = str2num(split{3});
g_high = str2num(split{4});
R = 8.314; % Gas Constant
for comment = 1:3 % Skipping the 3 comment lines
skip = fgetl(file);
end
t = linspace(200,3500,1000); % Temperature array
dir_name = \'parced NASA data\';
mkdir(dir_name)
for i = 1:51
[name,num1,num2,num3]= extract_coeff(file); % Extracting data
hightemp = [num1 num2(1:2)]; % Low and high temp coeffs
lowtemp = [num2(3:end) num3];
[Cp]= specific_heat(hightemp,lowtemp,t,g_med); % Specific Heat
[H]=enthalpy(hightemp,lowtemp,t,g_med); % Enthalpy
[S]=entropy(hightemp,lowtemp,t,g_med); % Entropy
[mw] =m_weight(name); % Molecular weight calculation
plot_therm(t,Cp,H,S,name,mw,dir_name) %temperature,specific heat,entropy plots
end
fclose(file);
2.
% FUNCTION FOR EXTRACTING COEFFIECIENTS FROM THE NASA DATA
function [spicies_name,number1,number2,number3] = extract_coeff(file)
% For Line 1
line = fgetl(file); % Line with species name
spicies_name = line(1:5);
% For Line 1
line1 = fgetl(file);
point = strfind(line1,\'.\'); % Finding . in line
number1 = zeros(1,5); % Creating Empty array
for i = 1:length(point)
number1(i) = str2num(line1(point(i)-2:point(i)+12));
% starts from 2 places before point and 12 places after point
end
% For Line 2
line2 = fgetl(file);
point = strfind(line2,\'.\');
number2 = zeros(1,5);
for i = 1:length(point)
number2(i) = str2num(line2(point(i)-2:point(i)+12));
end
% For Line 3
line3 = fgetl(file); % Line with species name
point = strfind(line3,\'.\');
number3 = zeros(1,4);
for i = 1:length(point)
number3(i) = str2num(line3(point(i)-2:point(i)+12));
end
end
3.
%FUNCTION FOR CALCULATING THE SPECIFIC HEAT
function [Cp]=specific_heat(high_t,low_t,t,g_med)
% Loop for Temperature array
for i=1:length(t) % For high Temperature
term1 = high_t(1);
term2 = high_t(2).*t(i);
term3 = high_t(3).*t(i)^2;
term4 = high_t(4).*t(i)^3;
term5 = high_t(5).*t(i)^4;
% For low temperature
term6 = low_t(1);
term7 = low_t(2).*t(i);
term8 = low_t(3).*t(i)^2;
term9 = low_t(4).*t(i)^3;
term10 = low_t(5).*t(i)^4;
% Calculate specific heat and comparing to global medium temp
if t(i)>=g_med
Cp(i)= term1 +term2 +term3 +term4 +term5;
else
Cp(i)= term6 +term7 +term8 +term9 +term10;
end
end
end
4.%FUNCTION FOR CALCULATING THE ENTHALPY
function [H]=enthalpy(high_t,low_t,t,global_med)
% Loop for Temperature array
for i=1:length(t) % For high temperature
term1 = high_t(1);
term2 = high_t(2).*(t(i)/2);
term3 = high_t(3).*(t(i)^2/3);
term4 = high_t(4).*(t(i)^3/4);
term5 = high_t(5).*(t(i)^4/5);
term6 = high_t(6)./t(i);
term7 = low_t(1); % For low temperature
term8 = low_t(2).*(t(i)/2);
term9 = low_t(3).*(t(i)^2/3);
term10 = low_t(4).*(t(i)^3/4);
term11 = low_t(5).*(t(i)^4/5);
term12 = low_t(6)./t(i);
% Calculate enthalpy and comparing to global medium temp
if t(i)>=global_med
H(i)= term1+term2+term3+term4+term5+term6;
else
H(i)= term7+term8+term9+term10+term11+term12;
end
end
end
5.
%FUNCTION FOR CALCULATING THE ENTROPY
function [S]=entropy(high_t,low_t,t,g_med)
% Loop for Temperature array
for i=1:length(t) % For High Temperature
term1 = high_t(1).*log(t(i));
term2 = high_t(2).*t(i);
term3 = high_t(3).*(t(i)^2)/2;
term4 = high_t(4).*(t(i)^3)/3;
term5 = high_t(5).*(t(i)^4)/4;
term6 = high_t(7);
term7 = low_t(1).*log(t(i)); % For low temperature
term8 = low_t(2).*t(i);
term9 = low_t(3).*(t(i)^2)/2;
term10 = low_t(4).*(t(i)^3)/3;
term11 = low_t(5).*(t(i)^4)/4;
term12 = low_t(7);
%Calculate entropy and comparing to global medium temp
if t(i)>=g_med
S(i)= term1+term2+term3+term4+term5+term6;
else
S(i)= term7+term8+term9+term10+term11+term12;
end
end
end
6.
%FUNCTION FOR CALCULATING THE MOLECULAR WEIGHT
function [mw] = m_weight(name)
elements = [\'H\', \'C\', \'N\', \'O\', \'A\'];
weights = [1 12 14 16 40]; % Elements and their corresponding molecular weights
mw = 0; % Initial molecular weight of compound
for i=1:length(name)
for j=1:length(elements)
if strcmp(name(i),elements(j))
mw = mw+weights(j);
index = j; %Storing the index of matched element
end
end
n = str2num(name(i));
if n>1
mw = mw+(weights(index)*n)-weights(index);
end
end
end
7.
%FUNCTION FOR PLOTTING THE TEMPERATURE ,ENTHALPY,ENTROPY,SPECIFIC HEAT
function plot_therm(t,Cp,H,S,name,mw,dir_name)
p = pwd; % Prints current working directory
cd(dir_name) % Change directory
ele_dir = sprintf(\'%s\',name); % making a directory with compound name
mkdir(ele_dir);
% Generating plots for Cp vs Temperature
% and switching off the display to reduce time
figure(1)
set(figure(1),\'visible\',\'off\')
plot(t,Cp,\'color\',\'k\',\'Marker\',\'o\',\'markersize\',3,\'markerfacecolor\',\'b\');
title(sprintf(\'Specific heat vs Temprature n Name: %s Molecular Weight: %d\',name,mw));
xlabel(\'Temperature\');
ylabel(\'Cp/R\');
% Generating plots for H vs Temperature
% and switching off the display to reduce time
figure(2)
set(figure(2),\'visible\',\'off\')
plot(t,H,\'color\',\'k\',\'Marker\',\'o\',\'MarkerFaceColor\',\'g\',\'markersize\',3);
title(sprintf(\'Enthalpy vs Temprature n Name: %s Molecular Weight: %d\',name,mw));
xlabel(\'Temperature\');
ylabel(\'H/RT\');
% Generating plots for Cp vs Temperature
% and switching off the display to reduce time
figure(3)
set(figure(3),\'visible\',\'off\')
plot(t,S,\'color\',\'k\',\'Marker\',\'o\',\'MarkerFaceColor\',\'y\',\'markersize\',3);
title(sprintf(\'Entropy vs Temprature n Name: %s Molecular Weight: %d\',name,mw));
xlabel(\'Temperature\');
ylabel(\'S/R\');
% Switiching between directories and saving plots
cd(ele_dir)
saveas(figure(1),\'Cp.jpg\');
saveas(figure(2),\'H.jpg\');
saveas(figure(3),\'S.jpg\');
cd(p)
end
1.Main program
fopen command is used to read the data from the thermo.txt file.
fgetl command is used to reading the global temperatures and strsplit is used to split the data .str2num is used to convert the string into numbers.
then a for loop is used to skip the lines in the thermo data.A for loop consisting of different functions of extracting the coefficents,specific heat,entropy,enthalpy,molecular wieght, plots ..
2.Function for extracting the coeffecients
A loop is created for reading the species name then another loop is created to read the number in that line and is converted from string to the number. This is repeated for the reamining lines.
3.Function for calculating the specific heat
A for loop for temperature array is created for storing the data for all the species and this loop itirates upto the length of t.The terms are calucated of formulaes .In this the first terms are considered for the high temperature and the last five terms are considerd for the low temperature. The highest value from temperature data is selected for calculation for first five terms and the lowest value from temperature data is selected for calculations for the last five terms And these terms are added and compared to the global medium temperature and that result is considered as sepcific heat and the process is repeated for all the species.
4.Function for calculating the enthalpy
The process of calculating the enthalpy is same as the process used for caluclating the specific heat except the formulaes and number of terms in the calucaltions varies compared to the specific heat calculations. The highest value from temperature data is selected for calculation for first six terms and the lowest value from temperature data is selected for last six tems for calculations And these terms are added and compared to the global medium temperature and that result is considered as enthalpy and the process is repeated for all the species.
5.Function for calculating the entropy
The process of calculating the entropy is same as the process used for caluclating the specific heat and enthalpy except the formulaes and number of terms in the calucaltions varies compared to the specific heat and enthalpy calculations. The highest value from temperature data is selected for calculation for first seven terms and the lowest value from temperature data is selected for calculations for the last seven terms And these terms are added and compared to the global medium temperature and that result is considered as entropy and the process is repeated for all the species.
6.Function for calculating the molecular weight
At first the element names and their weights are given .Its initial molecular weight is taken as zero . A for loop is created to compare the name of the given species and the element and the weights are added.Then the index of the matched names is stored. Then that string is converted into the number using str2num command.
7.Funciton for plotting the temperatures,specific heat,enthalpy,entropy
A directory is created and it is changed into the directory with the name of the spiecies . Then the plots are created for temperature,specific heats ,enthalpy,entropy and made to store in their folders .After the generation of the plots of the specified species ,the visibility sets off .The figures are saved in thier folders with their names on the plots..