Parsing NASA thermodynamic data
Parsing NASA thermodynamic data
AIM:
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. You will be reading this file NASA thermodynamic data.
OBJECTIVE:
Calculate the molecular weight of each species and display it in the command window.
Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.
Save the plots as images with appropriate names and dump them in separate folders for each species with suitable name. All these should be generated automatically.
FORMULA USED:
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
R - UNIVERSAL GAS CONSTANT
T - TEMPERATURE
The First 7 coefficients are High-temperature coefficients and the Second 7 coefficients are Low temperature coefficients
FILE PARSING
Parsing is the process of analyzing 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(CP):
The Specific heat is the amount of heat per unit mass required to raise the temperature by one degree celsius.
Enthalpy(H):
When a substance changes at constant pressure,enthalpy tells how much heat and work was added or removed from the substance.Enthalpy is similar to energy,but not the same.When a
substance grows or shrinks,energy is used up or released.
Entropy(S):
The entropy of an object is a measure of the amount of energy which is unavailable to do work.Entropy is the also a measure of the number of possible arrangements the atoms in a
system can have.In this sense,entropy is the measure of uncertainity or randomness.
Molecular Weight:
The average mass of a molecule of a compound compared to 1/12 the mass of carbon 12 and calculated as the sum of the atomic weight of the constituent atoms.
Molecular weight of a species is given by the sum of atomic weights of the atoms making up the species.
Atomic weights of the atoms (in ) is given below :
H - 1.00784
O - 15.999
C - 12.0107
N - 14.0067
Ar - 39.948
The following commands are to be used to open and navigate within a text file in MATLAB :
fopen - To Open a File [NOTE: file to be in same location as working folder]
fgetl - To navigate to the next line within the opened file
strsplit - To split a string into parts
str2num - To convert a string into number [ Note : If it is cell array, then {} to be used ]
findstr - To find the position of a particular string within a larger string
pwd - To Display current directory path
mkdir - To create a new directory
cd - To navigate to a different directory
openfig - To open a saved .fig file in MATLAB input - To get input from user
strcmp- To compare strings disregarding their cases
feof - to test end of a file
frewind - to move get beggining of file
main program for finding no of species
frewind(f1);
tline=0;
while ~feof(f1)
line=fgetl(f1);
a=sscanf(line,'%c');
%checking for end
c=a(1:3);
d='END';
if c==d
break
end
tline=tline+1;
end
%calculating the no of species
no_species=(tline-5)/4;
main program for extract coefficients
clear all
close all
clc
%reading the file data
f1 = fopen('THERMO.dat','r');
line_1 = fgetl(f1);
line_2 = fgetl(f1);
%extract global temprature range
A=strsplit(line_2,' ');
global_low_temp = str2num(A{2});
global_mid_temp = str2num(A{3});
global_high_temp = str2num(A{4});
% skipping unwanted line
for i = 1:3
omit = fgetl(f1);
end
frewind(f1);
tline=0;
while ~feof(f1)
line=fgetl(f1);
a=sscanf(line,'%c');
%checking for end
c=a(1:3);
d='END';
if c==d
break
end
tline=tline+1;
end
%calculating the no of species
no_species=(tline-5)/4;
frewind(f1);
for i=1:5
fgetl(f1);
end
for i = 1:no_species
line_3 = fgetl(f1);
line_3_split = strsplit(line_3,' ');
species_name = line_3_split(1);
species_name = species_name{1};
splits = strsplit(line_3,'G ');
lt = str2num(splits{2});
lt_low = lt(1);
lt_mid = lt(3);
lt_high = lt(2);
% co efficients
line_4 = fgetl(f1);
c = findstr(line_4,'E');
a1 = str2num(line_4(1:c(1)+3));
a2 = str2num(line_4(c(1)+4:c(2)+3));
a3 = str2num(line_4(c(2)+4:c(3)+3));
a4 = str2num(line_4(c(3)+4:c(4)+3));
a5 = str2num(line_4(c(4)+4:c(5)+3));
line_5=fgetl(f1);
a6=str2num(line_5(1:c(1)+3));
a7=str2num(line_5(c(1)+4:c(2)+3));
a8=str2num(line_5(c(2)+4:c(3)+3));
a9=str2num(line_5(c(3)+4:c(4)+3));
a10=str2num(line_5(c(4)+4:c(5)+3));
line_6=fgetl(f1);
a11=str2num(line_6(1:c(1)+3));
a12=str2num(line_6(c(1)+4:c(2)+3));
a13=str2num(line_6(c(2)+4:c(3)+3));
a14=str2num(line_6(c(3)+4:c(4)+3));
% calculating the values of Cp,enthalpy and entrophy
% universal gas constant
T=linspace(lt_low,lt_high,100);
R=8.314;
Cp= specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,lt_mid,T,R);
H= enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,lt_mid,T,R);
S= entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,lt_mid,T,R);
% calculating the molecular weight of species
Molecular_weight(i)=molecular_weight(species_name);
%plotting
mkdir(species_name);
figure(1)
plot(T,Cp,'linewidth',3,'color','black')
xlabel('Temperature')
ylabel('Specific heat')
title (species_name)
figure(2)
plot(T,H,'linewidth',3,'color','black')
xlabel('Temperature')
ylabel('Enthalpy')
title(species_name)
figure(3)
plot(T,S,'linewidth',3,'color','black')
xlabel('Temperature')
ylabel('Entropy')
title(species_name)
cd(species_name)
saveas(figure(1),'T vs Cp.jpg')
saveas(figure(2),'T vs H.jpg')
saveas(figure(3),'T vs S.jpg')
cd ('..')
% printing molecular weight data in command window
fprintf('species name : %s n',species_name)
fprintf('Molecular weight : %s n',Molecular_weight(i))
end
program for specific heat function
function Cp = specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,lt_mid,T,R);
if(T>lt_mid)
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
program for enthalpy function
function H = enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,lt_mid,T,R);
if(T>lt_mid)
H = (a1+a2*T./2+a3*T.^2./3+a4*T.^3./4+a5*T.^4./5+a6./T)*R.*T;
else
H = (a8+a9*T./2+a10*T.^2./3+a11*T.^3./4+a12*T.^4./5+a13./T)*R.*T;
end
program for entropy function
function S = entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,lt_mid,T,R);
if(T>lt_mid)
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
program for molecular weight function
function [Molecular_weight] = molecular_weight(species_name)
Atomic_mass = [15.999,1.00784,32.065,12.0107,14.0067,39.948];
Elements = ['O','H','S','C','N','A'];
Molecular_weight = 0;
u = 0;
for i = 1:length(species_name)
for j = 1:length(Elements)
if(strcmp(species_name(i),Elements(j)))
Molecular_weight = Molecular_weight+Atomic_mass(j);
u = j;
end
end
end
num=str2double(species_name(i));
if(num>1)
Molecular_weight = Molecular_weight+Atomic_mass(u)*(num-1);
end
end
command window showing molecular weights

folder of species in current folder

plot of O2



plot for N2



plot for CO2



EXPLANATION:
- First i transfer the dowloaded thermo.dat file in to my current folder
- Then opened the file using fopen command and encode every line by using fgetl command
- To encode globel temperature i used strsplit and str2num commands
- Using frewind and foef command read the file
- using while loop calculate the total no of lines and no of species usng the formula mentioned in program
- To encode the coeffieients i used strsplit command for spliting THE String by keeping 'E' as the reference
- To encode the species name i used strsplit curvebractkets of respective line that had species name as putting 'space ' as the reference
- Same procedure is followed to encode first 14 coefficients and everything is put in to 'for' command for encode all 53 species information
- Seperate function programs have coded for entropy,enthalpy , specific heat and molecular weight
EXPLANATION TO FUNCTION CODES
1. The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients
2. so i used if and else conditions for calculating respective coeffients with respective formulas of entropy ,enthalpy and specific heat
conclusion
The THERMO.dat file which is released by the NASA has been parsed successfully with necessary calculations and plots Using MATLAB