Project

Parsing NASA thermodynamic data

objective

  1. To write a program that extracts the 14 co-efficients and calculates the enthalpy, entropy
    and specific heat for all the species in the data file.
  2. To Calculate the molecular weight of each species and display it in the command
    window.
  3. To Plot the Cp, Enthalpy and Entropy for the local temperature range (specific for each
    species).
  4. To save the plots as images with appropriate names and dump them in separate folders
    for each species with suitable name.

THEORY:

file parcing;

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:

The Specific heat is the amount of heat per unit mass required to raise the temperature by one degree celsius.


Enthalpy:

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:

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.

formulas


The Formulas for calculating the entropy,enthalpy and specific heat is given below

Here,


R - Universal Gas Constant - 8.3145 J/K-mole


T - Temperature in K


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

main program for parsing 14 coefficients

clear all
close all
clc


f1 = fopen('THERMO.dat','r');
line_1 = fgetl(f1);
line_2 = fgetl(f1);

A=strsplit(line_2,' ');
global_low_temp = str2num(A{2});
global_mid_temp = str2num(A{3});
global_high_temp = str2num(A{4});

for i = 1:3
    omit = fgetl(f1);
end

for i = 1:53
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

function code for enthalpy

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

function code for entropy

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

function code for specific heat

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

function code for molecular weight

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

display of molecular weight in command window

species of folder in current folder

plots of O2

plots of N2

plots of CO2

EXPLANATON TO THE MAIN PROGRAM

  1. First i transfer the dowloaded thermo.dat file in to my current folder
  2. Then opened the file using fopen command and encode every line by using fgetl command
  3. To encode globel temperature i used strsplit and str2num commands
  4. To encode the coeffieients i used strsplit command for spliting THE String by keeping 'E' as the reference
  5. To encode the species name i used strsplit curvebractkets of respective line that had species name as putting 'space ' as the reference
  6. Same procedure is followed to encode first 14 coefficients and everything is put in to 'for' command for encode all 53 species information
  7. Seperate function programs have coded for entropy,enthalpy and specific heat respectively

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 relesed by the NASA has been parsed successfully with necessary calculations and plots Using MATLAB

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.