Parsing NASA's Thermodynamic Data
AIM
To parse NASA’s Thermodynamic data from the given data file.
OBJECTIVES
- To extract the 14 coefficients and calculate the Enthalpy, Entropy and Specific Heats for all the species in the data file.
- To calculate the molecular weight of each species and display it in the command window.
- To plot the Cp, Enthalpy and Entropy for the local temperature range,specific for each species.
- To save the plots as images with appropriate names and dump them in separate folders for each species with suitable name.
THEORY
Parsing is the process of breaking a data block into smaller chunks by following a set of rules, so that it can be more easily interpreted, managed, or transmitted by a computer.
The coefficients extracted from the file are used to obtain thermodynamic properties using the following relations.
where,
Cp = Specific Heat
H = Enthalpy
S = Entropy
R = Universal Gas Constant
T = Temperature
(a1,a2,a3,a4,a5,a6,a7) = Coefficients
CODE
Programming Language used- MATLAB
For parsing and extracting data
clear all
close all
clc
%Opening the file and getting the global temperatures
nasa=fopen('THERMO.dat','r');
header=fgetl(nasa);
global_temp=fgetl(nasa);
gt=strsplit(global_temp,' ');
global_low=str2double(gt{2});
global_mid=str2double(gt{3});
global_high=str2double(gt{4});
%Skipping next 3 lines(comments)
for i = 1:3
fgetl(nasa);
end
%extracting the species name and coefficients for all the 53 molecules
for i=1:53
species=fgetl(nasa);%reading the line containing the species name
x=strsplit(species,' ');
species_name=x{1};%first entry is the name of species
%local temperature values are 2nd,3rd,4th value from last
local_low_temp=str2double(x{end-3});
local_high_temp=str2double(x{end-2});
local_mid_temp=str2double(x{end-1});
%Calculating coefficients
%Reading the lines containing coefficients
l1=fgetl(nasa);
l2=fgetl(nasa);
l3=fgetl(nasa);
%Finding coefficients
a=strfind(l1,'E');
b=strfind(l2,'E');
c=strfind(l3,'E');
%Extracting High temperature coefficients
a1=str2double(l1(1:a(1)+3));
a2=str2double(l1(a(1)+4:a(2)+3));
a3=str2double(l1(a(2)+4:a(3)+3));
a4=str2double(l1(a(3)+4:a(4)+3));
a5=str2double(l1(a(4)+4:a(5)+3));
a6=str2double(l2(1:b(1)+3));
a7=str2double(l2(b(1)+4:b(2)+3));
%Extracting Low temperature coefficients
b1=str2double(l2(b(2)+4:b(3)+3));
b2=str2double(l2(b(3)+4:b(4)+3));
b3=str2double(l2(b(4)+4:b(5)+3));
b4=str2double(l3(1:c(1)+3));
b5=str2double(l3(c(1)+4:c(2)+3));
b6=str2double(l3(c(2)+4:c(3)+3));
b7=str2double(l3(c(3)+4:c(4)+3));
%Creating folder for each species
mkdir('C:\Users\Abhishek Mohanty\File parsing',species_name);
%Tempearture range
T=linspace(local_low_temp,local_high_temp,100);
R=8.314;%Universal Gas Constant( J/mol K)
%Specific Heat,Enthalpy and Entropy
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;%Specific heat
h=(a1.*T+(a2.*(T.^2))./2+(a3.*(T.^3))./3+(a4.*(T.^4))./4+(a5.*(T.^5))./5+a6).*R;%Enthalpy
s=(a1.*log(T)+a2.*T+(a3.*(T.^2))./2+(a4.*(T.^3))./3+(a5.*(T.^4))./4+a7).*R;%Entropy
else
cp=(b1+b2.*(T)+b3.*(T.^2)+b4.*(T.^3)+b5.*(T.^4)).*R;%Specific heat
H=(b1.*T+(b2.*(T.^2))./2+(b3.*(T.^3))./3+(b4.*(T.^4))./4+(b5.*(T.^5))./5+b6).*R;%Enthalpy
S=(b1.*log(T)+b2.*T+(b3.*(T.^2))./2+(b4.*(T.^3))./3+(b5.*(T.^4))./4+b7).*R;%Entropy
end
end
%Plotting Specific Heat vs Temperature
figure(1)
plot(T,cp,'linewidth',3,'color','r');
xlabel('Temperature(in Kelvin)');
ylabel('Specific Heat(in KJ/kg K)');
title(sprintf('Specific Heat vs Temperature for %s',species_name));
%Plotting Enthalpy vs Temperature
figure(2)
plot(T,H,'linewidth',3,'color','b');
xlabel('Temperature(in Kelvin)');
ylabel('Enthalpy(in KJ/kg)');
title(sprintf('Enthalpy vs Temperature for %s',species_name));
%Plotting Entropy vs Temperature
figure(3)
plot(T,S,'linewidth',3,'color','y');
xlabel('Temperature(in Kelvin)');
ylabel('Entropy(in KJ/mol K)');
title(sprintf('Entropy vs Temperature for %s',species_name));
directory=(['C:\Users\Abhishek Mohanty\File parsing\',species_name]);
cd(directory);%changing file path to save the plot
%Saving the generated plot
saveas(figure(1),'Cp.png');
saveas(figure(2),'H.png');
saveas(figure(3),'S.png');
%Switching back the directory
cd('C:\Users\Abhishek Mohanty\File parsing');
%Calculating Molecular weight of each species
M=molecular_weight(species_name);
endfor
fclose(nasa)
For function ‘molecular_weight’
%Function to calculate the molecular weight
function M=molecular_weight(species_name)
elements={ 'H', 'C', 'N', 'O', 'AR'};
atomic_weight=[1 12 14 16 40];
M=0;
for i=1:length(species_name)
for j=1:length(atomic_weight)
if strcmp(species_name(i),elements(j))==1
M=M+atomic_weight(j);
u=j;
endif
endfor
%adding the weight of repeating atom
n=str2double(species_name(i));
if n>1
M=M+atomic_weight(u)*(n-1);
endif
endfor
fprintf('Species- %s\n',species_name);
fprintf('Molecular weight- %d\n',M);
endfunction
OUTPUT
Molecular weight of species displayed in command window
1.
2.
3.
Location of saved folders for each species
Plots for O2, N2 and CO2
1.O2
2.N2
3.CO2
EXPLANATION
Code-
For extracting the Thermodynamic data
- First the given thermodynamic data file ‘THERMO.dat’ is opened, read and stored in the variable ‘nasa’.
- Then the header is skipped using fgetl() command.
- The global temperatures are extracted using strsplit() command by separating them on basis of location of space(‘ ‘) and then using str2double() to get double data values.
- The next 3 lines are skipped using fgetl() as they are comments.
- A for loop is initiated that would iterate through 53 cycles to extract data regarding each species.
- Using fgetl(), strsplit()and str2double() commands the value of local low, mid and high temperature as well as species name are stored in the variables.
- Then the next three lines containing the coefficients are fetched and stored using fgetl() command.
- In each line, the coefficients are separated on the basis of occurence of ‘E’ by using strfind() command.
- Then the 14 coefficients are extracted by using str2double() command from the strings by identifying the start and end of characters on either side of ‘E’.
- The coefficients are thus stored into respective variables.
- A folder is created using mkdir() command to store the species data.
- The temperature range is defined from the local low temperature to local high temperature using linspace() command.
- After extracting the coefficients, the Specific heat, Entropy and Enthalpy is calculated by taking gas constant R=8.314 J/mol-k and using a for loop to for all temperature values and if-else clause to assign the coefficients based on the value of temperature(greater than or less than local mid temperature)
- On finding the values of Specific Heat, Enthalpy and Entropy, graphs can be plotted between Specific heat-Temperature, Enthalpy-Temperature and Entropy -Temperature for each species.
- The three plots are stored in a folder having the name of the species.
- The ‘cd()’ command is used to revert the folder prior to every iteration so that each folder has plot for its own species.
- Finally molecular mass of each species is calculated and displayed in command window.
For function ‘molecular_weight’
- This function for molecular weight calculations requires species name as input argument and the output argument is molecular weight.
- A string array is created which contains the name of the elements.
- A variable atomic_weight is created which contains the atomic weight of respective atoms in the string array.
- Then the strcmp() command is used to compare the name of the element with the string array using a if clause under a for loop for each species..
- If there is repetition of any element, str2double() is used to find that and (n-1) is multiplied to atomic_weight of element to calculate molecular weight.
Output-
The plots and molecular weights were obtained for all species and the behaviour of thermodynamic properties were noticed.
ERRORS ENCOUNTERED
The file location was wrong. As the directory is to be changed a ‘\’ was missing in the end.
CONCLUSION
So, the parsing of NASA’s Thermodynamic data was successfully done.