Project 1 - Parsing NASA thermodynamic data
Aim:
Write a code in Matlab to parse the NASA thermodynamic data file and then calculate thermodynamic properties of various gas species.
Objective:
1. 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.
2. Calculate the molecular weight of each species.
3. Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.
File Parsing:
Parsing,syntax analysis, or syntactic analysis is the process of analysing a string of symbol, either in natural language, computer languages or data structures, conforming to the rules of formal grammer. The term parsing comes from Latin pars (orationis), meaning part(of speech). Within computational linguistics the term is used to refer to the formal analysis by a computer of a sentence or other string of words into its constituents, resulting in a parse tree showing their syntactic relation to each other, which may also contain semantic and other information.
Parsing a file means reading in a data stream of some sort and building an in memory model of the semantic content of the data. THis aims at faciliting performing some kind of transformation on the data.
NASA Thermodynamic File:
NASA thermodynamic file which is THERMO.dat file in which there are temperature range and 53 species are given in these data and each species contains 14 coefficients and temperature ranges and we have to pars these 14 coefficients to find Specific heat, Enthalpy, Entropy using formula as,
where,
R = Universal Gas constant
T = Temperature
Cp = Specific Heat (Kj)
S = Enthalpy (Kj/mol-K)
H = Entropy (Kj/mol)
a(i) = Coefficients where, i = 1 to14
Specific Heat:
The specific heat is the amount of heat per unit mass required to raise the temperature by one degree Celcius. The relationship between heat and temperature change is usually expressed in the form shown below where c is the specific heat.
Cp = R*(a1+(a2*T)+(a3*T.^2)+(a4*T.^3)+(a5*T.^4))
Enthalpy:
Enthalpy, a property of a thermodynamic system, is equal to the system internal energy plus the product of its preessure and volume. In a system contained so as to prevent mass transfer, for processes at costant pressure, the heat absorbed or released equals the changen in enthalpy.
H = R*T.*(a1+((a2*T)/2)+((a3*(T).^2)/3)+((a4*(T).^3)/4)+((a5*(T).^4)/5)+(a6./T))
Entropy:
Entropy, the measure of a system 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.
S = R*(a1.*log(T)+(a2*T)+((a3*(T).^2)/2)+((a4*(T).^3)/3)+((a5*(T).^4)/4)+a7)
Steps:
- Firstly, we will input the parameter R and we will open a file THERMO.dat file and reading this file using fopen command.
- Then, we will read first THERMO using fgetl command and then we read temperature file and we will split the temperatures using strsplit command to convert it into cell array and then we will convert this string to number using str2num command to find low, high and mid temperatures.
- Then we will assign temperature range using linspace command. Then we run loop to skip command lines the data file using fgetl command inside the loop.
- Now, we will run the loop to find 53 element in the data file by assinging fgetl command and then splitting into the cell array to find the species line and then find the speecies name and then find the number of the temperatures in the species line.
- Then we read the new line. In order to read the coefficients in the data file we will first find the 'E' string using strfind command to find before and after numbers of the coefficients then we will find remaining 14 coefficients and convert them to numbers from strings.
- Out of which we will find the first 7 coefficients which are for higher temperature and then second 7 coefficients which are for lower temperature.
- Now we will specify Specific heat Cp, Enthalpy H, Entropy S, functions to find these parameters using these 14 coefficients for lower temperature. then we will specify molecular weight W function to find the Molecular weight of the different species.
- Now we will write the molecular weigth.txt file and fprintf all the molecular weights of the species. and the close the txt file using fclose command.
- Now we change the directory using cd command and the creating directory folder where we have to save our files using mkdir command and then we will change the directory to this created folder.
- Now, we will plpt Specific heat, Enthalpy, ntropy with the temperature for different species. We will use scanf command to in the title command to create different species title.
- Then we will use saveas command to save the plots of different species in the directory folder. Now we will change the directory to which our main code file runs in MATLAB.
Main Program
clear all
close all
clc
% Opening of NASA's thermodynamic data
f1=fopen('THERMO.dat','r');
first_line=fgetl(f1);
%Obtaining of global max,min and medium temperatures
global_temp = fgetl(f1);
temp_range = strsplit(global_temp,' ');
global_min_temp = str2num(temp_range{2})
global_med_temp = str2num(temp_range{3})
global_max_temp = str2num(temp_range{4})
%Range of temperature
T = linspace(global_min_temp,global_max_temp,400);
R = 8.314
%skipping the next three commentsin the file
for i = 3:5
comments = fgetl(f1);
end
%Getting the coeffs of species
for i = 1:53
%Extracting line 1 and the name of the species
line1 = fgetl(f1);
name = strtok(line1);
%Extracting higher and lower temperature coeffs
line2 = fgetl(f1);
line3 = fgetl(f1);
line4 = fgetl(f1);
A = strfind(line2,'E');
B = strfind(line3,'E');
C = strfind(line4,'E');
%Extracting higher temperature coeffs
a1 = str2num(line2(1:A(1)+3));
a2 = str2num(line2(A(1)+4:A(2)+3));
a3 = str2num(line2(A(2)+4:A(3)+3));
a4 = str2num(line2(A(3)+4:A(4)+3));
a5 = str2num(line2(A(4)+4:A(5)+3));
a6 = str2num(line3(1:B(1)+3));
a7 = str2num(line3(B(1)+4:B(2)+3));
%Extracting lower temperature coeffs
b1 = str2num(line3(B(2)+4:B(3)+3));
b2 = str2num(line3(B(3)+4:B(4)+3));
b3 = str2num(line3(B(4)+4:B(5)+3));
b4 = str2num(line4(1:C(1)+3));
b5 = str2num(line4(C(1)+4:C(2)+3));
b6 = str2num(line4(C(2)+4:C(3)+3));
b7 = str2num(line4(C(3)+4:C(4)+3));
%Specific heat calculation
cp = specificheat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,T,R,global_med_temp);
%Enthalpy calculation
h = enthalpy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,T,R,global_med_temp);
%Entropy calcultion
s = entropy_calc(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,T,R,global_med_temp);
%Calcultion molecular weight of species
Molecular_weight(i) = molecular_weight(name)
%Creating individual folder for each species and plotting
plotting(cp,h,s,T,name)
end
Plotting Program
%Creating plotting function
function plotting(cp,h,s,T,name)
cd(pwd)
dir_name = name;
mkdir(dir_name);
figure(1)
plot(T,cp,'linewidth',2,'Color','b')
title(sprintf('Specific heat Vs Temperature for %s',name))
xlabel('TEMPERATURE [K]')
ylabel('SPECIFIC HEAT[KJ]')
grid on
saveas(figure(1),'Specific Heat vs.png')
figure(2)
plot(T,h,'linewidth',2,'Color','r')
title(sprintf('Enthalpy Vs Temperature for %s',name))
xlabel('TEMPERATURE[k]')
ylabel('ENTHALPY[kJ/mol')
grid on
saveas(figure(2),'Enthalpy.png')
figure(3)
plot(T,s,'linewidth',2,'Color','g')
title(sprintf('Entropy Vs Temperature for %s',name))
xlabel('TEMPERATURE[K]')
ylabel('ENTROPY[KJ/(mol-K)]')
grid on
saveas(figure(3),'Entropy.png')
end
Calculation of Molecular weight
% Function to calculate molecular weight
function MW = molecular_weight(name)
atoms = ['H','C','O','N','R'];
fprintf('Molecular Weight of');
fprintf(name)
fprintf(' : ');
atomic_weight = [1 12 16 14 40];
MW = 0'
for i = 1:length(name)
for j =length(atoms)
if strcmp(name(i),atoms(j))
MW = MW+atomic_weight(j);
position = j;
end
end
n = str2num(name(i));
if n>1
MW = MW+atomic_weight(j)*(n-1);
end
end
fprintf('%d',MW);
disp(' ');
end
Calculation of Specific Heat
%Calcultion of specific heat
function cp = specificheat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,T,R,global_med_temp)
%Comparing with global temp
%If less than global it will take min coeffs
if T > global_med_temp
cp = R.*(a1+a2.*T+a3.*T.^2+a4.*T.^3+a5.*T.^4);
%If temp is more it will take max coeffs
else
cp = R.*(b1+b2.*T+b3.*T.^2+b4.*T.^3+b5.*T.^4);
end
Calculation of Enthalpy
%Caculating of Enthalpy
function h = enthalpy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,T,R,global_med_temp)
if T > global_med_temp
h = R.*(a1.*T+(a2.*T.^2)./2+(a3.*T.^3)./3+(a4.*T.^4)./4+(a5.*T.^5)./5+a6);
else
h = R.*(b1.*T+(b2.*T.^2)./2+(b3.*T.^3)./3+(b4.*T.^4)./4+(b5.*T.^5)./5+b6);
end
Calculation of Entropy
% Calculation of Entropy
function s = entropy_calc(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,T,R,global_med_temp)
if T<=global_med_temp
s = R*(a1.*log(T)+(a2.*T)+(a3.*(T.^2))./2+(a4.*(T.^3))./3+(a5.*(T.^4))./4+a7);
else
s = R*(b1.*log(T)+(b2.*T)+(b3.*(T.^2))./2+(b4.*(T.^3))./3+(b5.*(T.^4))./4+b7);
end
Output Molecular weight

Output Specific Heat (N2,O2,CO2 Species)



Output Enthalpy (N2,O2,CO2 Species)



Output Entropy (N2,O2,CO2 Species)



Screenshot of the directory where different species are saved
