Project 1 - Parsing NASA thermodynamic data
AIM
To write a program that extracts co-efficient and temperatures from NASA thermodynamic data and to calculate the enthalpy, entropy and specific heats for all the species in the data file.
OBJECTIVE
- Write a function that extracts the 14 co-efficient for a given species
- Write functions that calculate the enthalpy, entropy and specitic heats using the NASA polynomials
- Wite a function that calculates the molecular weight of each species 4 Wite a function that plots the Cp
- Write a function that plots the Cp. Enthalpy and Entropy of an input species
- Save the plots with appropriate names.
- Display useful plots (enthalpy, entropy and cp) for 02, N2 and Co2.
THEORY
Parsing is the process of analysing text made of a sequence of tokens to determine its grammatical structure with respect to a given (more or less) formal grammar. The parser then builds a data structure based on the tokens. parsing is a compiler that is used to break the data into smaller elements coming from lexical analysis phase. A parser takes input in the form of sequence of tokens and produces output in the form of parse tree.
Specific heat : The specific heat (also called specific heat capacity) is the amount of heat required to change a unit mass (or unit quantity, such as mole) of a substance by one degree in temperature. Therefore, unlike the extensive variable heat capacity which depends on the quantity of material, specific heat is an intensive variable and has units of energy per mass per degree (or energy per number of moles per degree). The heat capacity of a substance can differ depending on what extensive variable are held constant, with the quantity being held constant usually being denoted with a subscript . For example, the specific heat at constant pressure is commonly denoted Cp.
Enthalpy, the sum of the internal energy and the product of the pressure and volume of a thermodynamic system. Enthalpy is an energy-like property or state function—it has the dimensions of energy, and its value is determined entirely by the temperature, pressure, and composition of the system and not by its history. In symbols, the enthalpy, H, equals the sum of the internal energy, E, and the product of the pressure, P, and volume, V, of the system: H = E + PV.
Entropy, the measure of a system’s thermal energy per unit temperature that is unavailable for doing useful work. Because work is obtained from ordered molecular motion, the amount of entropy is also a measure of the molecular disorder, or randomness, of a system.
Molecular weight is a measure of the sum of the atomic weight values of the atoms in a molecule. Molecular weight is used in chemistry to determine stoichiometry in chemical reactions and equations. Atomic mass is a basic physical property of matter. The mass of an atom or a molecule is referred to as the atomic mass. The atomic mass is used to find the average mass of elements and molecules and to solve stoichiometry problems.

Main Program Coding
clear all
close all
clc
% Reading the thermodynamic data file
f1 = fopen('THERMO.dat','r');
file_name = fgetl(f1);
t_line = fgetl(f1);
A = strsplit(t_line,' ');
% Defining the temperatures extracted from data file
global_low_temp = str2double(A{2});
global_mid_temp = str2double(A{3});
global_high_temp = str2double(A{4});
for i = (1:3)
comments = fgetl(f1);
end
for i = 1:53
% Extracting species name
species = fgetl(f1);
G = strsplit(species,' ');
species_name = G{1};
% Extracting coefficients from first line
line_1 = fgetl(f1);
l1 = strfind(line_1,'E');
a1 = str2double(line_1(1:(l1(1)+3)));
a2 = str2double(line_1((l1(1)+4):(l1(2)+3)));
a3 = str2double(line_1((l1(2)+4):(l1(3)+3)));
a4 = str2double(line_1((l1(3)+4):(l1(4)+3)));
a5 = str2double(line_1((l1(4)+4):(l1(5)+3)));
% Extracting coefficients from second line
line_2 = fgetl(f1);
l2 = strfind(line_2,'E');
a6 = str2double(line_2(1:(l2(1)+3)));
a7 = str2double(line_2((l2(1)+4):(l2(2)+3)));
a8 = str2double(line_2((l2(2)+4):(l2(3)+3)));
a9 = str2double(line_2((l2(3)+4):(l2(4)+3)));
a10 = str2double(line_2((l2(4)+4):(l2(5)+3)));
% Extracting coefficients from third line
line_3 = fgetl(f1);
l3 = strfind(line_3,'E');
a11 = str2double(line_3(1:(l3(1)+3)));
a12 = str2double(line_3((l3(1)+4):(l3(2)+3)));
a13 = str2double(line_3((l3(2)+4):(l3(3)+3)));
a14 = str2double(line_3((l3(3)+4):(l3(4)+3)));
% Creating a folder 'SPECIES' to save thermodynamic plots of species
%mkdir(['C:DOCUMENTS', species_name]);
T = linspace(global_low_temp, global_high_temp, 500);
% calling the thermodynamic equations function
[cp, h, s] = coeff(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T);
% Plotting
% Plot between Temperature and Specific Heat
figure(1)
plot(T,cp,'linewidth',3,'color','b')
title (species_name)
xlabel('TEMPERATURE')
ylabel('SPECIFIC HEAT')
% Plot between Temperature and Enthalpy
figure(2)
plot(T,h,'linewidth',3,'color','g')
title(species_name)
xlabel('TEMPERATURE')
ylabel('ENTHALPY')
% Plot between Temperature and Entropy
figure(3)
plot(T,s,'linewidth',3,'color','c')
title(species_name)
xlabel('TEMPERATURE')
ylabel('ENTROPY')
mkdir('C:\Users\sarju\OneDrive\Documents\File Parsing',species_name)
cd(species_name)
% Saving plots
saveas(1,'specific_heat.png')
saveas(2,'enthalpy.png')
saveas(3,'entropy.png')
% Changing directory for calculating molecular weight
cd('C:\Users\sarju\OneDrive\Documents\File Parsing')
molecular_weight = mole_wt(species_name);
end
fclose(f1);
Main Program Explanation
- First I defined the variable R(Universal gas constant) with a value of 314 J / mol·K Opened the data file using fopen command and assigned to a variable f1 which acts like a pointer for the file THERMO.dat
- Using fgetl command read and saved the Header line and global temperature values.
- Then splited the global temperature values using strsplit and in the data file using str2double command to split the data from the string.
- Then the next 3 line are not neccesary they defined only the command / text for their understanding and for that a for loop is used for three similar opeartion to the lines.
- For loop is used to read the co-efficient of all 53 species Then the fgetl command reads, splitted and saved into the variable species Using the strfind the 'G' character is found in the line and with respect to this G taken the values of Local low, mid, high temperature values Using the similar approach as above found out the 14 co-efficient for different species .
- Strings are converted into numerical by using str2double
- Use strfind command to locate the character of 'E' in each line .
- Co-efficient are assigned to the variables a1,a2,a3……..a14 which are also has been provided in the above function programs Temperature is set with an array of Local low temperature to Local high temperature with 250 number of division using linspace
- For different temperature range function calls and values of Cp ,H and S are calculated for temperatures
- Using mkdir function we have created new folder with name for all the output files Cd command changes the directory again
- By using plot and figures commands plots have been created for all the different species saveas command is used to save all the plots into their respective folders
- Then called the molecular weight function program and prints the values of all the species in the command window
- Below are the main code, function code used for Specific Heat, Enthalpy, Entropy and the molecular weight calculation code.
Function Program For Molecular Weight
function [molecular_weight] = mole_wt(species_name)
SPECIES = species_name;
constituents = {'H','C','N','O','A'};
atomic_weight = [1 12 14 16 40];
molecular_weight = 0;
for i = 1:length(species_name)
for j = 1:length(constituents)
if strcmp(SPECIES(i),constituents(j))
molecular_weight = molecular_weight + atomic_weight(j);
u = j;
end
end
n = str2double(SPECIES(i));
if n > 1
molecular_weight = molecular_weight + atomic_weight(u)*(n-1);
end
end
fprintf('molecular_weight of %s:- ',species_name)
fprintf('%d',molecular_weight)
fprintf(' ')
fprintf('amu')
fprintf('\n')
end
EXPLANATION OF FUNCTION TO CALCULATE THE MOLECULAR WEIGHT OF ALL ELEMENTS
This function for molecular weight calculations requires 'species name' as input argument and it is saved in the variable 'MM' and the output argument is molecular mass.
- A string array is created which contains the name of the atoms.
- A variable Atomic_weight is created which contains the atomic weight of respective atoms in the string array.
- Constituents = ['H', 'C', 'N', 'O', 'Ar'];
- Then used ‘strcmp’ command to compare the name of the element with the string array.
- If there is a repetition of an element, ‘str2num’ is used to find that and (n-1) is multiplied to Atomic weight of the element to calculate molecular weight.
FUNCTION FOR EXTRACTING CO-EFFICIENTS
function [cp, h, s] = coeff(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T)
R = 8.314;
for j = 1:length(T)
if T(j) < 300
cp = (a1 + a2.*T + a3.*T.^2 + a4.*T.^3 + a5.*T.^4).*R;
h = (a1 + (a2.*T)./2 + (a3.*T.^2)./3 + (a4.*T.^3)./4 + (a5.*T.^4)./5 + (a6./T)).*R.*T;
s = (a1.*log(T) + a2.*T + (a3.*T.^2)./2 + (a4.*T.^3)./3 + (a5.*T.^4)./4 + a7).*R;
else
cp = (a8 + a9.*T + a10.*T.^2 + a11.*T.^3 + a12.*T.^4).*R;
h = (a8 + (a9.*T)./2 + (a10.*T.^2)./3 + (a11.*T.^3)./4 + (a12.*T.^4)./5 + (a13./T)).*R.*T;
s = (a8.*log(T) + a9.*T + (a10.*T.^2)./2 + (a11.*T.^3)./3 + (a12.*T.^4)./4 + a14).*R;
end
end
end
Here is the function for extracting the 14 coefficients. Every specie in the data is having 14 coefficients, and out of these 14 first seven (1-7) are low temperature coefficients and next seven (8-14) are high temperature coefficients. Here fgetl is used to get the line for the coefficients. Coefficients are distributed in three lines, 1st line containing first five coefficients, 2nd line containing next five coefficients and the 3rd line containing next 4 coefficients. Now to extract the coefficient we use strfind command to find the E character in the coefficient. This was done because letter E common in all the coefficients. Now through this 'E' letter 1st co efficient was extracted through the line. And all other coefficient was extracted from the line. After this another line was called using fgetl command and by the same way all the five coefficient were extracted of that line and similarly coefficients of next line were extracted. coefficients of line 1 (a_1, a_2, a_3, a_4, a_5) coefficients of line 2 (b_1, b_2, b_3, b_4, b_5) coefficients of line 3 (c_1, c_2, c_3, c_4) All these coefficients were stored in "F"
COMMAND WINDOW SHOWING MOLECULAR WEIGHT


SCREENSHOT OF FOLDER CONTAINING ALL FILES

ERRORS WHILE CODING
There are several erros faced during coding.
The main error occured if the variable G is nor defined

After defining the variable the program run smoothly.
RESULT
Thus MATLAB program is created to write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.
All the required plots are attached and the screenshot showing the file is also uploaded.
REFERENCES
FILE PARSING USING MATLAB : Skill-Lync
What Is Parsing of Data? - Blog | Oxylabs