PARSING NASA THERMODYNAMIC DATA
1.AIM :
Write a MATLAB program to
- extract the 14 co-efficients and calculate the enthalpy, entropy and specific heats for all the species in the data file.
- Calculate the molecular weight of each species.
- Plot the Cp, Enthalpy and Entropy for the local temperature range.
2.OBJECTIVES :
- To read the NASA thermodynamic data file and write a function to extract the 14 co-efficients and calculate the enthalpy, entropy and specific heats for all the species in the data file.
- Calculate and display the molecular weight of each species.
- Plot the Cp, Enthalpy and Entropy for the local temperature range specific for each species.
3.THEORY :
FILE PARSING: File parsing in MATLAB means reading the data from an external file or writing the data to an external file, the data read can be used to process into the information.
Some of file parsing and data manipulation commands in MATLAB which are used in the program too are given below:
1)fileID = fopen(filename,permission)
fileID = fopen(filename, permission) opens the file with the type of access specified by permission.
2)‘r’ and 'w'
str at the delimiters specified by delimiter.A and interprets values in the file according to the format specified by formatSpec.a1, a2, a3, a4, a5, a6, and a7 are the numerical coefficients available in the NASA thermodynamic files, the first 7 numbers starting on the second line of each species entry are the seven coefficients for the high temperature range and the next seven numbers are the coefficients for the low temperature range.
The distribution of data in the file is shown below
The data file provides 14 coefficients for each species and the temperature ranges over which the fits are valid.
The important input to calculation of specific heat, enthalpy, and entropy is temperature, there are 2 ranges given in datafile global temperature range and local temperature range, the common temperature connecting the two ranges is 1000 K.
Procedure:
- Initially the NASA thermodynamic data file named 'THERMO.dat' is open and the data is read from the file and a pointer to the file is assigned.
- Skip the starting header line from the datafile which are not useful.
- Extract the global temperature range and seperate the value of low, mid and high temperature.
- Skip the next 3 header lines from the datafile which are not useful.
- Then extract the 14 coefficients for each species present in the given data file.
- Once the coefficients are extracted for a species, then calculate the entropy, enthalpy and specific heat, assuming universal gas constant R=8.314 J/mol-k and assuming lower to higher temperature range.
- Once the entropy, enthalpy and specific heat gets calculated for all the species, plot the graph between Temperature vs Entropy, Temperature vs Enthalpy and Temperature vs Specific heat.
- Store the plots in a folder having the same name as of the element.
- Finally calculate the molecular mass of each element and display in command window.
4.MATLAB PROGRAM :
The MATLAB program for the required objective is given below:-
close all
clear all
clc
%opening NASA Thermodynamic data file and reading data
file = fopen('THERMO.dat','r'); %Open file 'THERMO.dat' in read mode
line1 = fgetl(file); %Read a line from file
%Obtaing Global temperature values
line2 = fgetl(file);
%extracting and skipping the next three non useful header in the file
des_1 = fgetl(file);
des_2 = fgetl(file);
des_3 = fgetl(file);
%spiltting string and converting to number
A = strsplit(line2,' ');
Global_low_temperature = str2num(A{2}); % Global Low temperature
Global_mid_temperature = str2num(A{3}); % Global Medium temperature
Global_high_temperature = str2num(A{4}); % Global High temperature
%extracting species information
for i=1:53
species= fgetl(file);
species_name = species(1:6)
%Extracting line 1 coefficients of the species
Line_1 = fgetl(file);
L1 = strfind(Line_1,'E');
c1 = str2num(Line_1(1:(L1(1)+3)));
c2 = str2num(Line_1((L1(1)+4):(L1(2)+3)));
c3 = str2num(Line_1((L1(2)+4):(L1(3)+3)));
c4 = str2num(Line_1((L1(3)+4):(L1(4)+3)));
c5 = str2num(Line_1((L1(4)+4):(L1(5)+3)));
%Extracting line 2 coefficients of the species
Line_2 = fgetl(file);
L2 = strfind(Line_2,'E');
c6 = str2num(Line_2(1:(L2(1)+3)));
c7 = str2num(Line_2((L2(1)+4):(L2(2)+3)));
c8 = str2num(Line_2((L2(2)+4):(L2(3)+3)));
c9 = str2num(Line_2((L2(3)+4):(L2(4)+3)));
c10 = str2num(Line_2((L2(4)+4):(L2(5)+3)));
%Extracting line 3 coefficients of the species
Line_3 = fgetl(file);
L3 = strfind(Line_3,'E');
c11 = str2num(Line_3(1:(L3(1)+3)));
c12 = str2num(Line_3((L3(1)+4):(L3(2)+3)));
c13 = str2num(Line_3((L3(2)+4):(L3(3)+3)));
c14 = str2num(Line_3((L3(3)+4):(L3(4)+3)));
%creating folder for each species
mkdir(['F:\matlab\programs\file parsing project\species\',species_name]);
%Temperature range assumed for calculation purpose
Temperature = linspace(300,5000,100);
%Universal gas constant
R =8.314;
%calculation of thermal properties based on the lower and upper temperature
for j = 1:length(Temperature)
if Temperature(j)<3000
%specific heat
Cp = (c1+c2.*Temperature+c3.*Temperature.^2+c4.*Temperature.^3+c5.*Temperature.^4).*R;
%enthalphy
H = (c1+(c2.*Temperature)./2+(c3.*Temperature.^2)/3+(c4.*Temperature.^3)/4+(c5.*Temperature.^4/5)+c6./Temperature).*Temperature.*R;
%entropy
S = (c1*log(Temperature)+c2.*Temperature+(c3.*Temperature.^2)/2+(c4.*Temperature.^3)/3+(c5.*Temperature.^4)/4+c7).*R;
else
%specific heat
Cp = (c8+c9.*Temperature+c10.*Temperature.^2+c11.*Temperature.^3+c12.*Temperature.^4).*R;
%enthalphy
H = (c8+(c9.*Temperature)/2+(c10.*Temperature.^2)/3+(c11.*Temperature.^3)/4+(c12.*Temperature.^4/5)+c13./Temperature).*Temperature.*R;
%entropy
S = (c8.*log(Temperature)+c9.*Temperature+(c10.*Temperature.^2)/2+(c11.*Temperature.^3)/3+(c12.*Temperature.^4)/4+c14).*R;
end
end
%plotting graphs
%Temperature vs Entropy
figure(1)
plot(Temperature,S,'linewidth',4,'color','b')
title(species_name)
xlabel('Temperature [K]')
ylabel('Entropy [kJ/(mol-K)]')
%Temperature vs Enthalpy
figure(2)
plot(Temperature,H,'linewidth',4,'color','k')
title(species_name)
xlabel('Temperature [K]')
ylabel('Enthalpy [kJ/(mol)]')
%Temperature vs Specific heat
figure(3)
plot(Temperature,Cp,'linewidth',4,'color','y')
title(species_name)
xlabel('Temperature [K]')
ylabel('Specific Heat [kJ]')
directory = (['F:\matlab\programs\file parsing project\species\',species_name]); %filepath to save the plots
cd(directory)
%saving plots
saveas(1,'Entropy.jpg');
saveas(2,'Enthalpy.jpg');
saveas(3,'Specific heat.jpg');
%switching the directory to the location of data file for molecular mass calculation
cd('F:\matlab\programs\file parsing project\')
MM = molecular_mass(species_name);
disp('Molecular weight : ')
disp(MM)
end
fclose(file) %close file
The code for the function molecular_mass() is given below:-
%function to calculate molecular mass
function MM = molecular_mass(species_name)
species = upper(species_name);
Constituents = ['H', 'C', 'N', 'O', 'A']; %species present in data file
Atomic_weight = [1.00794 12.0107 14.0067 15.9994 39.948]; %standard values
MM = 0; %initializing
for i=1:length(species)
for j=1:length(Constituents)
if strcmp(species(i),Constituents(j))
MM = MM + Atomic_weight(j);
u = j;
end
end
n = str2num(species(i));
if n>1
MM = MM + Atomic_weight(u)*(n-1);
end
end
end
Explanation:-
- At first the data file i.e Thermo.dat is opened using ‘fopen()’ command in 'r’ i.e read mode.
- 'fgetl()' command is used to read the first line, but the header is of no use.
- Again the 'fgetl()' command is used to read the global temperature range line, this temperature range is in form of a string so to split this 'strsplit()' command is used and further the small strings obtained is converted to number using 'str2num()' command and global low, medium and high temperature is found.
- Again using the 'fgetl()' command 3 times the 3 header files which are useless are extracted.
- Further a temperature range is assumed and an array is created using 'linspace()' command.
- To extract 14 coefficients and calculate the thermodynamic quantities for all the 53 species the 'for' loop is used.
- In he loop the species name and coefficients extracted and specific heat, enthalphy and entropy is calculated.
- Further the Temperature vs Entropy, Temperature vs Enthalpy and Temperature vs Specific heat graph are plotted and saved in a folder.
- Using the 'molecular_mass()' function the molecular weight of each species is calculated.
- For closing the file 'fclose()' command is used.
5.RESULTS :
Plot for o2 species:-
Plot for N2 species:-
Plot for Co2 species:-
Screenshot of the window where all the folders are saved:-
Molecular weight of each species:-
Errors faced:-
1)Wrong syntax of command 'fgetl()'
2) Wrong syntax of command 'str2num()'
6.CONCLUSION :
File parsing of NASA Thermodynamics data file was done and thermodynamic quantites were calculated and plotted, further molecular weight was also calculated.