File Parsing - NASA thermodynamics data
Objective
To write a function/program that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.
Plot the graphs of Cp vs Temperature, H vs Temperature and S vs Temperature also save these plots in specific folders automatically.
To calculate and display the molecular weight of all the species.
Data
`(Cp)/R = a1 + a2*T+ a3 *T^2+ a4*T^3 + a5*T^5`
`H/(R*T) = a1 + (a2*T)/2 + (a3*T^2)/3 + (a4*T^3)/4 + (a5*T^4)/5 + (a6)/T`
`S/R = a1*log(T) + a2*T + (a3*T^2)/2 + (a4*T^3)/3 + (a5*T^4)/4 + a7`
Theory
In the data each species has 14 temperature coefficients. The first seven are the high temperature coefficient and the next seven are low temperature coefficient. Also for each specie there is unique temperate range known as the local temperature range.
Molecular Weight - The sum of atomic masses of all the atoms in the molecule is known as the molecular mass or molecular weight. E.g. Hydrogen(H) atomic weight = 1.008 and Carbon(C) atomic weight = 12.0107. CH2 is a molecule which consists 1 Carbon atom and 2 Hydrogen atom, therefore its molecular weight is (12.0107)+(1.0008)*2 = 14.0123. Its SI unit is dalton named after John Dalton.
Specific Heat - the quantity of heat required to raise the temperature of one gram of a substance by one Celsius degree is known as the Specific Heat. It is denoted by Cp. The units of specific heat are usually Calories or Joules per gram per Celsius degree.
Enthalpy - When a process occurs at constant pressure, the heat evolved (either released or absorbed) is equal to the change in enthalpy. Enthalpy is the sum of the internal energy and the product of pressure and volume. It is denoted by H. SI unit of enthalpy is Joules per Kilogram.
Entropy - It is a thermodynamic quantity representing the unavailability of a system's thermal energy for conversion into mechanical work, often interpreted as the degree of disorder or randomness in the system. It is denoted by S. Its SI unit is Joule per Kelvin.
Program
Main Program
clear all
close all
clc
% Opening the file
f = fopen('THERMO.dat','r');
% File Heading
heading = fgetl(f);
% Getting the Global Temperature
global_temp = fgetl(f);
global_temperature = strsplit(global_temp,' ');
% The global temperature array is a string and we need to convert it into
% numbers
global_low_temperature = str2double(global_temperature{2});
global_mid_temperature = str2double(global_temperature{3});
global_high_temperature = str2double(global_temperature{4});
% Skipping the next 3 comment lines
for i = 1:3
comment = fgetl(f);
end
% Getting the species
% There are total of 53 species in the data, each having 14 temperature
% coefficients and specific local temperature.
for j = 1:53
species = fgetl(f);
% Getting the species names
species_name = species(1:6);
% Getting the local temperatures
species_rows = strfind(species,'G');
% Finding G to know the positions of the temperatures
local_low_temp = str2double(species(46:55));
local_high_temp = str2double(species(56:65));
local_mid_temp = str2double(species(66:75));
% Getting the 14 temperature coefficients
line1 = fgetl(f);
row1 = strfind(line1,'E');
a1 = str2double(line1(1:row1(1)+3));
a2 = str2double(line1(row1(1)+4:row1(2)+3));
a3 = str2double(line1(row1(2)+4:row1(3)+3));
a4 = str2double(line1(row1(3)+4:row1(4)+3));
a5 = str2double(line1(row1(4)+4:row1(5)+3));
line2 = fgetl(f);
a6 = str2double(line2(1:row1(1)+3));
a7 = str2double(line2(row1(1)+4:row1(2)+3));
a8 = str2double(line2(row1(2)+4:row1(3)+3));
a9 = str2double(line2(row1(3)+4:row1(4)+3));
a10 = str2double(line2(row1(4)+4:row1(5)+3));
line3 = fgetl(f);
a11 = str2double(line3(1:row1(1)+3));
a12 = str2double(line3(row1(1)+4:row1(2)+3));
a13 = str2double(line3(row1(2)+4:row1(3)+3));
a14 = str2double(line3(row1(3)+4:row1(4)+3));
% Creating new folder to save species data
mkdir(['C:UsersaddypDesktopthermofileFile Parsing THERMO',species_name])
% R is universal gas constant
R = 8.314;
% Creating local temperature matrix for ploting the graphs
T = linspace(local_low_temp,local_high_temp,100);
% Function for Specific Heat
Specific_Heat = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,local_mid_temp,R,T);
% Function for Enthalpy
Enthalpy = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,local_mid_temp,R,T);
% Function for Entropy
Entropy = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,local_mid_temp,R,T);
% Plotting the graphs
% Specific Heat vs Local Temperature
figure(1)
plot(T,Specific_Heat,'linewidth',2,'color','r')
title(species_name)
xlabel('Local Temperature')
ylabel('Specific Heat')
% Enthalpy vs Local Temperature
figure(2)
plot(T,Enthalpy,'linewidth',2,'color','b')
title(species_name)
xlabel('Local Temperature')
ylabel('Enthalpy')
% Entropy vs Local Temperature
figure(3)
plot(T,Entropy,'linewidth',2,'color','g')
title(species_name)
xlabel('Local Temperature')
ylabel('Entropy')
% Changing the directory to save the plots in the specific folder
cd(['C:UsersaddypDesktopthermofileFile Parsing THERMO',species_name])
saveas(1,'Specific Heat.jpg')
saveas(2,'Enthalpy.jpg')
saveas(3,'Entropy.jpg')
% Redirecting the directory where the code is saved.
% If this step is not done our code will not run.
cd(['C:UsersaddypDesktopthermofile'])
% Function for molecular weight
Molecular_Weight = molecular_weight(species_name);
end
function Cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,local_mid_temp,R,T)
for j = 1:length(T)
if T(j) > local_mid_temp
Cp = (a1 + a2*T + a3*T.^2 + a4*T.^3 + a5*T.^4)*R;
else
Cp = (a8 + a9*T + a10*T.^2 + a11*T.^3 + a12*T.^4)*R;
end
end
end
function H = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,local_mid_temp,R,T)
for j = 1:length(T)
if T(j) > local_mid_temp
H = (a1 + (a2*T)/2 + (a3*T.^2)/3 + (a4*T.^3)/4 + (a5*T.^4)/5 + a6*T)*R.*T;
else
H = (a8 + (a9*T)/2 + (a10*T.^2)/3 + (a11*T.^3)/4 + (a12*T.^4)/5 + a13*T)*R.*T;
end
end
function S = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,local_mid_temp,R,T)
for j = 1:length(T)
if T(j) > local_mid_temp
S = (a1*log(T) + a2*T + (a3*T.^2)/2 + (a4*T.^3)/3 + (a5*T.^4)/4 + a7)*R;
else
S = (a8*log(T) + a9*T + (a10*T.^2)/2 + (a11*T.^3)/3 + (a12*T.^4)/4 + a14)*R;
end
end
end
function mm = molecular_weight(species_name)
elements = ['H','C','O','N','A'];
atomic_weight = [1.0079, 12.0107, 15.999, 14.0067, 39.948];
% Considering intial molecular mass = 0
mm = 0;
for x = 1:length(species_name)
for y = 1:length(atomic_weight)
if strcmp(species_name(x),elements(y))
mm = mm + atomic_weight(y) ;
position = y;
end
end
n = str2double(species_name(x));
if n > 1
mm = mm + atomic_weight(position)*(n-1);
end
end
sprintf('Molecular Weight of %s:%f',species_name,mm)
end

















