File parsing using NASA thermodynamic DATA
Aim:
To write a function that extracts the 14 coefficients and calculates the enthalpy, entropy and specific heats for all the species in the data file
Explanation:
- Claiming the Universal Gas constant R as input.
- In order to get the temperature values, first open the file using 'fopen' to load the file from the directory.
- 'fgetl' is used to get the values of the temperature.
- The next is to find the low and high-temperature values for which 'linspace' is used.
- Then each species is saved in a variable 'species_N'.
- Two different coefficients are created using 'strfind' where now is for high-temperature coefficient and another for low-temperature coefficient.
- The different functions for different variables like Specific heat, enthalpy, entropy, and molecular weight are coded and called in the main program.
Theoretical definitions:
File Parsing:
Parsing a file means reading a data stream of some sort and building an in-memory model of the semantic content of data. This aims at facilitating performing some kind of transformation on data, It means to a computer "reading text" means to a human being. if we scan it, from start to finish and figure out what it means as you go - whether you're a person reading text or a computer parsing a file.
Specific Heat:
The specific heat is the amount of heat per unit mass required to raise the temperature by one-degree celsius.
Enthalpy:
Enthalpy is a thermodynamic property of a system. It is the sum of the internal energy added to the pressure and volume of the system. It reflects the capacity to do non-mechanical work and the capacity to release the heat. Enthalpy is denoted as H; specific enthalpy denoted as h.
Entropy:
The entropy of an object is a measure of the amount of energy that is unavailable to do work. Entropy is also a measure of the number of possible arrangements the atoms in the system can have. In this sense, entropy is a measure of uncertainty or randomness.
Molecular weight:
The average mass of a molecule of a compound compared to ¹/₁₂ the mass of carbon 12 and calculated as the sum of the atomic weights of the constituent atoms.
Atomic mass:
An atomic mass (symbol: ma) is the mass of a single atom of a chemical element. It includes the masses of the 3 subatomic particles that make up an atom: protons, neutrons, and electrons. Atomic mass can be expressed in grams.
Equations:
Empirical equation given to find the thermodynamic properties are,
Where,
Cp = Specific Heat(J/(K kg)).
Universal gas constant R=8.314 J / (mol:K).
H=Enthalpy(J).
S=Entropy(J/K).
T= Temperature(K).
Program:
Primary program for executing the functions and to work on given data:
clear all
close all
clc
% Input Details
% Gas Const(R)
R = 8.314;
% Data file
data = fopen('THERMO.dat', 'r');
% Header data file
fgetl(data);
% Datas on Global temperature
global_temp_range = fgetl(data);
a = strsplit(global_temp_range);
global_low_temp = 200;%str2double (a{2});
global_intermediate_temp = 1000;%str2double (a{3});
global_high_temp = 3500;%str2double (a{4});
temp = linspace(global_low_temp, global_high_temp, 400)
for i = 1:3
fgetl(data)
end
for i = 1:53
line1 = fgetl(data)
A = strsplit(line1, ' ');
species_N = (A{1});
t2 = fgetl(data);
t3 = fgetl(data);
t4 = fgetl(data);
j = strfind(t2, 'E');
k = strfind(t3, 'E');
l = strfind(t4, 'E');
% For high temperature coefficients
h1 = str2double(t2(1:j(1)+3));
h2 = str2double(t2(j(1)+4:j(2)+3));
h3 = str2double(t2(j(2)+4:j(3)+3));
h4 = str2double(t2(j(3)+4:j(4)+3));
h5 = str2double(t2(j(4)+4:j(5)+3));
h6 = str2double(t3(1:k(1)+3));
h7 = str2double(t3(k(1)+4:k(2)+3));
% For low temperature coefficients
n1 = str2double(t3(k(2)+4:k(3)+3));
n2 = str2double(t3(k(3)+4:k(4)+3));
n3 = str2double(t3(k(4)+4:k(5)+3));
n4 = str2double(t4(1:l(1)+3));
n5 = str2double(t4(l(1)+4:l(2)+3));
n6 = str2double(t4(l(2)+4:l(3)+3));
n7 = str2double(t4(l(3)+4:l(4)+3));
h=[h1 h2 h3 h4 h5 h6 h7 n1 n2 n3 n4 n5 n6 n7];
% Specific Heat Function
[c_p] = specific_heat(h1,h2,h3,h4,h5,n1,n2,n3,n4,n5,R,temp,global_intermediate_temp);
%
% % Enthalpy
[H] = enthalpy(h1,h2,h3,h4,h5,h6,n1,n2,n3,n4,n5,n6,R,temp,global_intermediate_temp);
%
% % Entropy
[S] = entropy(h1,h2,h3,h4,h5,h6,h7,n1,n2,n3,n4,n5,n6,n7,R,temp,global_intermediate_temp);
%[H]=enthalp_func(h,1000,temp,R)
mkdir(species_N);
current_dir = pwd;
cd(species_N);
% Plotting
figure(1)
plot(temp, c_p, 'linewidth', 3, 'color', 'r');
title(species_N)
xlabel('TEMPERATURE');
ylabel('SPECIFIC HEAT');
figure(2)
plot(temp,H,'linewidth', 3, 'color', 'g');
title(species_N)
xlabel('TEMPERATURE');
ylabel('ENTHALPY');
figure(3)
plot(temp,S,'linewidth', 3, 'color', 'b');
title(species_N)
xlabel('TEMPERATURE');
ylabel('ENTROPY');
saveas(1,'Specific_heat.png')
saveas(2,'Enthalpy.png')
saveas(3,'Entropy.png')
cd(current_dir);
m_w(i)= molecular_weight(species_N);
disp(m_w(i))
end
Function program for calculating Enthalpy:
% Function for Enthalpy
function [H] = enthalpy(h1,h2,h3,h4,h5,h6,n1,n2,n3,n4,n5,n6,R,temp,global_intermediate_temp)
for i = 1:length(temp)
if temp(i) >= global_intermediate_temp
H =(h1 + (h2.*(temp./2)) + (h3.*(temp.^2./2)) + (h4.*(temp.^3./3)) + (h5.*(temp.^4./4)) + (h6./temp(i))).* R.*temp;
else
H = (n1 + (n2.*(temp./2)) + (n3.*(temp.^2./2)) + (n4.*(temp.^3./3)) + (n5.*(temp.^4./4)) + (n6./temp(i))).*R.*temp;
end
end
end
Function program for calculating the Entropy:
% Function for Entropy
function [S] = entropy(h1,h2,h3,h4,h5,h6,h7,n1,n2,n3,n4,n5,n6,n7,R,temp,global_intermediate_temp);
for i = 1:length(temp)
if temp(i) <= global_intermediate_temp
S(i) = R.*((h1.*log10(temp(i))) + (h2.*temp(i)) + (h3.*(((temp(i)).^2)./2)) + (h4.*(((temp(i)).^3)./3)) + (h5.*(((temp(i)).^4./4))+h7));
else
S(i) = R.*((n1.*log10(temp(i))) + (n2.*temp(i)) + (n3.*(((temp(i)).^2)./2)) + (n4.*(((temp(i)).^3)./3)) + (n5.*(((temp(i)).^4/4))+n7));
end
end
end
Function program for calculating the Molecular weight:
% Function for Molecular Weight
function [m_w] = molecular_weight(species_N)
elements = {'H', 'C', 'O', 'N', 'A', 'S'};
atomic_weight = [1.007, 12.011, 15.999, 14.006, 39.948, 32.064];
m_w = 0
for i = 1:length(species_N)
for j = 1:length(elements)
if strcmp(species_N(i), elements(j));
m_w = (m_w + atomic_weight(j));
p = j;
end
end
q = str2double(species_N(i));
if q>1
m_w = m_w + atomic_weight(p)*(q-1);
end
end
end
Function program for calculating the specific_heat:
% Function for Specific Heat
function [c_p] = specific_heat(h1,h2,h3,h4,h5,n1,n2,n3,n4,n5,R,temp,global_intermediate_temp);
for i = 1:length(temp)
if temp(i) <= global_intermediate_temp
c_p(i) = R*(h1+(h2*temp(i)) + (h3*(temp(i)).^2) + (h4*(temp(i)).^3) + (h5*(temp(i)).^4));
else
c_p(i) = R*(n1+(n2*temp(i)) + (n3*(temp(i)).^2) + (n4*(temp(i)).^3) + (n5*(temp(i)).^4));
end
end
end
PLOTS:
Not able to show all the plots here are the two example plots and all the plots folder are taken as a screenshot and displayed below.
HCOOH:
O2:
Proof:
I have run this program in GUI Octave and the below image shows the execution of the program
Result:
Finally, the program is executed and the required results are obtained.