Project 1 - Parsing NASA thermodynamic data
Code with comments:
clear all
close all
clc
% Inputs (universal gas constant)
r = 8.314;
% Reading file data from 'THERMO.dat' file:
f1 = fopen('THERMO.dat','r');
a = fgetl(f1);
% Reading temperature from 'THERMO.dat' file:
temp = fgetl(f1);
%
% % Splitting the data (as the data is avaliable in one complete string format)
% s = strsplit(temp);
%
% % Converting the string data to variables we cannot use it directly in code
% templow = str2double(s{2});
% tempmid = str2double(s{3});
% temphigh = str2double({4});
% Range of temperature
% T = linspace(templow,temphigh,1000);
% skip the first 3 lines
for i = 1:3;
a = fgetl(f1);
end
for j = 1
tline = fgetl(f1);
A = strsplit(tline,' ');
species = (A{1});
templow1 = str2double(A{end-3});
temphigh1 = str2double(A{end-2});
tempmid1 = str2double(A{end-1});
tline1 = fgetl(f1);
a = strfind(tline1,'E');
% Finding higher temperature coefficients
a1 = tline1(1:a(1)+3);
a1 = str2double(a1);
a2 = tline1(a(1)+4:a(2)+3);
a2 = str2double(a2);
a3 = tline1(a(2)+4:a(3)+3);
a3 = str2double(a3);
a4 = tline1(a(3)+4:a(4)+3);
a4 = str2double(a4);
a5 = tline1(a(4)+4:a(5)+3);
a5 = str2double(a5);
tline2 = fgetl(f1);
b = strfind(tline2,'E');
a6 = tline2(1:b(1)+3);
a6 = str2double(a6);
a7 = tline2(b(1)+4:b(2)+3);
a7 = str2double(a7);
% Finding lower temperature coefficients
a8 = tline2(b(2)+4:b(3)+3);
a8 = str2double(a8);
a9 = tline2(b(3)+4:b(4)+3);
a9 = str2double(a9);
a10 = tline2(b(4)+4:b(5)+3);
a10 = str2double(a10);
tline3 = fgetl(f1);
c = strfind(tline3,'E');
a11 = tline3(1:c(1)+3);
a11 = str2double(a11);
a12 = tline3(c(1)+4:c(2)+3);
a12 = str2double(a12);
a13 = tline3(c(2)+4:c(3)+3);
a13 = str2double(a13);
a14 = tline3(c(3)+4:c(4)+3);
a14 = str2double(a14);
t = linspace(templow1, temphigh1, 1000)
for k = 1:length(t)
% To calculate specific heat
cp(k) = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,t(k),r,tempmid1);
% to calculate entropy
S(k) = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,t(k),r,tempmid1);
% to calculate enthaply
H(k) = enthaply(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,t(k),r,tempmid1);
end
% to calculate molecular weight
w(k) = molecular_weight(species);
% writing molecular weight to the file
mw_file = fopen('molecular_weight.txt','w');
fprintf(mw_file,'molecular weight of %s is %d',species,w);
fclose(mw_file);
% Graphs
% 1.) cp vs T
figure(1)
plot(t,cp,'linewidth',2,'color','b')
xlabel('Temperature(K)')
ylabel('Specific Heat(KJ)')
title(sprintf('specific heat vs temperature range of %s',species))
grid on
% 2.) s vs t
figure(2)
plot(t,S,'linewidth',2,'color','r')
xlabel('Temperature(K)')
ylabel('Entropy(KJ/mol-K)')
title(sprintf('Entropy vs temperature range of %s',species))
grid on
% 3.) h vs t
figure(3)
plot(t,H,'linewidth',2,'color','g')
xlabel('Temperature(K)')
ylabel('Enthaply(KJ/mokl)')
title(sprintf('Enthaply vs temperature range of %s',species))
grid on
end
function cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,r,tempmid1)
% Calculating specific heat for higher and lower temperature
if (T < tempmid1)
cp = r*(a8 + (a9*T) + (a10*(T).^2) + (a11*(T).^3) + (a12*(T).^4))
else
cp = r*(a1 + (a2*T) + (a3*(T)^2) + (a4*(T)^3) + (a5*(T)^4))
end
end
function s = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,r,tempmid)
% calculating entropy for higher and lower temperature
if (T <= tempmid)
s = r*(a8*log(T) + (a9*T) + ((a10*(T)^2)/2) + ((a11*(T)^3)/3) + ((a12*(T)^4)/4) + a14);
else
s = r*(a1*log(T) + (a2*T) + ((a3*(T)^2)/2) + ((a4*(T)^3)/3) + ((a5*(T)^4)/4) + a7);
end
end
function W = molecular_weight(species)
% defining the species
atomic_name = ['H','C','O','N','Ar'];
% defining the atomic mass
atomic_mass = [1.008 12.011 15.999 14.007 39.948];
% Input
W = 0;
for i = 1:length(species)
for j = 1:length(atomic_name)
if strcmp(species(i), atomic_name(j));
W = W + atomic_mass(j);
position = j;
end
end
n = str2double(species);
if n>1
W = W + atomic_mass(position)*(n-1);
end
end
% To display the molecular weight
fprintf('Molecular Weight of %s is : ',species)
fprintf('%f',W)
disp(' ')
end
function h = enthaply(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,r,tempmid)
% calculating enthalpy for higher and lower temperature ranges
if (T <= tempmid)
h = r*T*(a8 + ((a9*T)/2) + ((a10*(T)^2)/3) + ((a11*(T)^3)/4) + ((a12*(T)^4)/5) + ((a13/T)));
else
h = r*T*(a1 + ((a2*T)/2) + ((a3*(T)^2)/3) + ((a4*(T)^3)/4) + ((a5*(T)^4)/5) + ((a6/T)));
end
end
Oututs:
1. co2



2.) o2



3.) N2



Errors:
1.) while writing the code forgot to close the bracket

2.) for lower temperature coefficients the variable call was not used properly:

Oututs:
1. co2



2.) o2



3.) N2



All species data:

Errors:
1.) while writing the code forgot to close the bracket

2.) for lower temperature coefficients the variable call was not used properly:

Logic:
File parsing:
Parsing a file means reading in a data stream and building a memory model of the content of that data.
here we have to read the data from the Thermo.dat file and evaluate the coefficients for specific heat, enthalpy, and entropy.
The constants used in the file are:
R - Universal gas constant
T - Temperature
Cp - Specific Heat
H - Enthalpy
S - Entropy
Specific Heat: In thermodynamics, the specific heat capacity (symbol cp) of a substance is the heat capacity of a sample of the substance divided by the mass of the sample. Specific heat is also sometimes referred to as massic heat capacity. Informally, it is the amount of heaat that must be added to one unit of mass of the substance in order to cause an increase of one unit in temperature. The SI unit of specific heat capacity is joule per kelvin per kilogram, J⋅kg−1⋅K−1.
The formula for our calculation is: cp = r*(a1 + (a2*T) + (a3*(T)^2) + (a4*(T)^3) + (a5*(T)^4))
Entropy: It is the measure of systems thermal energy per unit temperature.
The formula is: s = r*(a1*log(T) + (a2*T) + ((a3*(T)^2)/2) + ((a4*(T)^3)/3) + ((a5*(T)^4)/4) + a7)
Enthalpy: is a property of thermodynamic system, and is defined as the sum of the system's internal energy and the product of its pressure and volume.
formulas is: h = r*T*(a1 + ((a2*T)/2) + ((a3*(T)^2)/3) + ((a4*(T)^3)/4) + ((a5*(T)^4)/5) + ((a6/T)))