Project 1 - Parsing NASA thermodynamic data
AIM : Parsing of NASA thermodynamics data file.
( Using Octave 4.0.0 GUI )
OBJECTIVE OF THE PROJECT:
Our main objective is to :
1.Write a function that can extracts all the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the given thermodynamic data file.
2.To calculate the molecular weight of each species.
3.To Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.
4.To save the plots as images with appropriate names and put them in separate folders for each species with suitable name.
ABOUT FILE PARSING :
Parsing means to take in a line of text, usually words with separators, extracting each word, and then semantically identifying the meaning of the sequence of words.
In this project we are given with NASA thermodynamic data for different species and each one of them have different temperature range and various properties. For this file, the formula for different properties are :
`(Cp)/(R) = a1 +a2xxT +a3xxT^2+a4xxT^3+a5xxT^4`
`(H)/(RT)=a1+a2xx(T)/2xxa3xxT^2/3xxa4xxT^3/4+a5xxT^4/5+(a6)/T`
`(S)/R=a1xxlnT+a2xxT+a3xxT^2/2+a4xxT^3/3+a5T^4//4 +a7`
Where, R = Universal Gas Constant.
T = Temperature.
The first 7 coefficients are high-temperature coefficients and the second 7 coefficients are low-temperature coefficients.
ABOUT NASA THERMODYNAMIC DATA FILE:
- First line gives us header information.
- Second line gives us global temperature range.
- Next three line are the comments section(started with exclamation sign) which gives information about the file.
- After that the next line tells the species name and local temperature range.(Denoted by "1" on the right hand side).
- Next three lines are the 14 coefficient (denoted by " 2 3 4" on the right hand side).
- After these the sequence follows the same order as point number 'd'.
FUNCTION FOR THE MOLECULAR WEIGHT:
% funtion to calculate molecular weight
function MW = Mol_wt(Species)
elements_name = ['H','C','O','N','A'];
element_weight= [1.008 12.011 15.999 14.007 39.948];
MW = 0;
for i = 1:length(Species)
for j = 1: length(elements_name)
if strcmpi(Species(i),elements_name(j))
MW = MW + element_weight(j);
d = j;
end
end
n = str2double(Species(i));
if n>1
MW = MW + element_weight(d)*(n-1);
end
end
end
MAIN PROGRAM:
%Parsing NASA thermodynamics file
clear all
close all
clc
%Opening the thermodynamics file
z1 = fopen('THERMO.dat','r');
%Header of the file
Header = fgetl(z1);
%GLobal temperature from the file
Global_temperature = fgetl(z1);
Y = strsplit(Global_temperature,' ');
global_low_temperature = str2num(Y{2}); %For global low temperature
global_medium_temperature = str2num(Y{3}); %For global medium temperature
global_high_temperature = str2num(Y{4}); %For global high temperature
%Skipping the comments section from the file
for i=3:5
comment = fgetl(z1);
end
for i = 1:53
line = fgetl(z1); %Reading line
p = strsplit(line, ' '); %Separating the variables
Species = p(1);
W = strfind(line,'G');
local_low_temp = line(W+3:55);
local_mid_temp = line(W+23:75);
local_high_temp = line(W+13:65);
low_temp = str2double(local_low_temp);
mid_temp = str2double(local_mid_temp);
high_temp = str2double(local_high_temp);
%Temperature range
T = linspace(low_temp,high_temp,800);
%Universal gas constsnt
R = 8.314;
%Temperature coefficient
%for the first line
line1 = fgetl(z1);
m = strfind(line1,'E');
%for the second line
line2 = fgetl(z1);
n = strfind(line2,'E');
%for the third line
line3 = fgetl(z1);
l = strfind(line3,'E');
%first 7 high temperature coefficient
a1 = str2num(line1(1:m(1)+3));
a2 = str2num(line1(m(1)+4:m(2)+3));
a3 = str2num(line1(m(2)+4:m(3)+3));
a4 = str2num(line1(m(3)+4:m(4)+3));
a5 = str2num(line1(m(4)+4:m(5)+3));
a6 = str2num(line2(1:n(1)+3));
a7 = str2num(line2(n(1)+4:n(2)+3));
%Second 7 low temperature coefficient
a8 = str2num(line2(n(2)+4:n(3)+3));
a9 = str2num(line2(n(3)+4:n(4)+3));
a10= str2num(line2(n(4)+4:n(5)+3));
a11= str2num(line3(1:l(1)+3));
a12= str2num(line3(l(1)+4:l(2)+3));
a13= str2num(line3(l(2)+4:l(3)+3));
a14= str2num(line3(l(3)+4:l(4)+3));
%Calculating various properties
%Entropy
if T > 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
%Enthapy
if T > mid_temp
H =(a1.*T+a2.*(T.^2)./2+a3.*(T.^3)./3+a4.*(T.^4)./4+a5.*(T.^5)./5+a6)*R;
else
H =(a8.*T+a9.*(T.^2)./2+a10.*(T.^3)./3+a11.*(T.^4)./4+a12.*(T.^5)./5+a13)*R;
end
%Specific heat
if T > 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
%Creating new file
Species=char(Species);
fd = sprintf('%s',Species);
cur_dir=pwd;
mkdir(Species);
cd(Species);
%Plotting
% Plot 1 for entropy vs temperature
figure(1);
plot(T,S,'linewidth',3,'color','r');
xlabel('Temperature[K]');
ylabel('Entropy[kJ/mol-k]');
title('Entropy vs Temperature');
grid on
saveas(gca, 'Entropy.png');
% Plot 2 for enthalpy vs temperature
figure(2);
plot(T,H,'linewidth',3,'color','b');
xlabel('Temperature[K]');
ylabel('Enthalpy[kJ/mol]');
title('Enthalpy vs Temperature');
grid on
saveas(gca,'Enthalpy.png');
% Plot 3 for specific heat vs temperature
figure(3);
plot(T,Cp,'linewidth',3,'color','g');
xlabel('Temperature[K]');
ylabel('Specific Heat[kJ]');
title('Specific Heat vs Temperature ');
grid on
saveas(gca, 'Specific Heat.png');
cd(cur_dir);
%Calculating molecular weight
MW = Mol_wt(Species);
fprintf('molecular weight of %s :',Species);
fprintf('%d',MW);
disp(' ');
end
OUTPUT:
1. PLOT OF O2:
2. PLOT FOR N2:
3. PLOT FOR CO2:
4. SCREENSHOT OF THE MOLECULAR WEIGHT:
5. SCREENSHOT OF THE FOLDER:
ERRORS:
1.Faced an error while plotting:
This was resolved by taking graphics_toolkit command.
2. Obtained same curve for the plots of different species as I was taking wrong local temperature.
CONCLUSION :
- From this program we have parse the NASA thermodynamic data file successfully.
- I have plot a graph of various properties.
- I have calculated the molecular mass of each species.
REFERENCE:
https://www.mathworks.com/help/matlab/ref/strsplit.html
http://www.thphys.nuim.ie/CompPhysics/matlab/help/techdoc/ref/fopen.html