Project

Parsing NASA thermodynamic data

Parsing NASA thermodynamic data

INTRODUCTION

To parse data or information means to break it down into component parts so that its syntax can be analyzed, categorized, and understood.

PARSER:A parser is a compiler or interpreter component that breaks data into smaller elements for easy translation into another language. A parser takes input in the form of a sequence of tokens or program instructions and usually builds a data structure in the form of a parse tree or an abstract syntax tree.

A parser is commonly used as a component of an interpreter or a compiler. The overall process of parsing involves three stages:

  1. Lexical Analysis: A lexical analyzer is used to produce tokens from a stream of input string characters, which are broken into small components to form meaningful expressions.
  2. Syntactic Analysis: Checks whether the generated tokens form a meaningful expression. This makes use of a context-free grammar that defines algorithmic procedures for components. These work to form an expression and define the particular order in which tokens must be placed.
  3. Semantic Parsing: The final parsing stage in which the meaning and implications of the validated expression are determined and necessary actions are taken

Parsers are widely used in the following technologies:

  • Java and other programming languages
  • HTML and XML
  • Interactive data language and object definition language
  • Database languages, such as SQL
  • Modeling languages, such as virtual reality modeling language
  • Scripting languages
  • Protocols, such as HTTP and Internet remote function calls

Here we are parsing the thermodynamic data formulated by NASA for various species.

In the code we will be parsing the data and calculating the coefficients for finding the specific heat,Enthalpy,Entropy for various species

FORMULAE

`(Cp)/R = a1 + a2 T + a3 T^2 + a4 T^3 + a5 T^4`

`H/(RT) = a1 + a2 T/2 + a3 T^2/3 + a4 T^3/4 + a5 T^4/5 + a6/T`

`S/R =a1 ln T+ a2 T + a3 T^2 /2 + a4 T^3 /3 + a5 T^4 /4 + a7`

CODE

clear all 
close all
clc


% universal gas constant
R = 8.314 ; 

%Reading header
f1 = fopen(\'THERMO.dat\',\'r\');
Header= fgetl(f1);

%Reading Global Temperature Line

Global_temp = fgetl(f1);
C = strsplit(Global_temp,\' \');
global_low_temp =str2double(C{2}); %Global Low Temperature 
global_mid_temp =str2double(C{3}); %Global Mid Temperature 
global_high_temp =str2double(C{4}); %Global High Temperature 

%%skipping 3 lines
fgetl(f1);
fgetl(f1);
fgetl(f1);

%using for loop to consider 53 elements

for i = 1:53
line= fgetl(f1); %Reading the line
A=strsplit(line,\' \'); %Seperating Variable
species=A(1); %Naming First Variable as \'Species\'
Q = strfind(line,\'G\'); 
local_low_temp = line(Q+3:55); 
local_high_temp = line(Q+13:65);
local_mid_temp = line(Q+23:75); 
lt = str2double(local_low_temp); %Local Low Temperature
ht = str2double(local_high_temp); %Local Mid Temperature 
mt = str2double(local_mid_temp); %Local High Temperature


%line 1-Reading the coefficient data

cfline1= fgetl(f1);
X = strfind(cfline1,\'E\');
a1 = cfline1(1:X(1)+3); 
a1 = str2double(a1); %storing 1st coefficient 
a2 = cfline1(X(1)+4:X(2)+3);
a2 = str2double(a2); %storing 2nd coefficient 
a3 = cfline1(X(2)+4:X(3)+3);
a3 = str2double(a3); %storing 3rd coefficient 
a4 = cfline1(X(3)+4:X(4)+3);
a4 = str2double(a4); %storing 4th coefficient 
a5 = cfline1(X(4)+4:X(5)+3);
a5 = str2double(a5); %storing 5th coefficient
 
%line 2-Reading coefficient data of 2nd line

cfline2 = fgetl(f1);
G = strfind(cfline2,\'E\');
a6 = cfline2(1:G(1)+3);
a6 = str2double(a6); %storing 6th coefficient 
a7 = cfline2(G(1)+4:G(2)+3);
a7 = str2double(a7); %storing 7th coefficient 
a8 = cfline2(G(2)+4:G(3)+3);
a8 = str2double(a8); %storing 8th coefficient 
a9 = cfline2(G(3)+4:G(4)+3);
a9 = str2double(a9); %storing 9th coefficient 
a10 = cfline2(G(4)+4:G(5)+3);
a10 = str2double(a10); %storing 10th coefficient 

%line 3-Reading coefficient data of 3rd line

cfline3 = fgetl(f1);
I = strfind(cfline3,\'E\');
a11 = cfline3(1:I(1)+3);
a11 = str2double(a11); %storing 11th coefficient 
a12 = cfline3(I(1)+4:I(2)+3);
a12 = str2double(a12); %storing 12th coefficient 
a13 = cfline3(I(2)+4:I(3)+3);
a13 = str2double(a13); %storing 13th coefficient 
a14 = cfline3(I(3)+4:I(4)+3);
a14=str2double(a14); %storing 14th coefficient 



%Ranging the temperature low temperature to high temperature

T=linspace(lt,ht,500);

%Specific Heat

Cp=specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt);

%Entropy

S=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt);

%Enthaphy

H=enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt);

%Saving as a folder
spec=char(species); 
fd = sprintf(\'%s\',spec);
cur_dir=pwd;
mkdir(spec);
cd(spec);
  
  
%Plot 1, Specific heat (Cp) vs Temperature (T)

figure(1);
plot(T,Cp,\'linewidth\',2,\'color\',\'r\');
xlabel(\'Temperature in K\');
ylabel(\'Specific heat in kJ\');
title(\'Specific heat vs Temperature\');
  
%Plot 2, Enthalpy (H) vs Temperature (T)
  
figure(2);
plot(T,H,\'linewidth\',2,\'color\',\'b\');
xlabel(\'Temperature in K\');
ylabel(\'Enthalpy in kJ/(mol)\');
title(\'Enthalpy vs Temperature\');
  
%Plot 3, Entropy (S) vs Temperature (T)

figure(3);
plot(T,S,\'linewidth\',2,\'color\',\'g\');
xlabel(\'Temperature in K\');
ylabel(\'Entropy in kJ/(mol K)\');
title(\'Entropy vs Temperature\');

% saving the plots that got generated

saveas(figure(1),\'cp.png\');
saveas(figure(2),\'h.png\');
saveas(figure(3),\'s.png\');
cd(cur_dir);
%Molecular mass
M= molecular_mass(species)

f2 = fopen(\'molecular mass\',\'w\');
fprintf(\'%s n %s n %f n\',\'species\',\'Molecular mass\',M)
fclose(f2)
end
 

fclose(f1);

Function for specific heat

function Cp=specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt)
if T>=mt
    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 for enthalpy

function H=enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt)
if T>=mt
    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

Function for entropy

function S=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt)
if T>=mt
    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 for molecular mass

%Function_for_Molecular_Weight%

function M= molecular_mass(species)
        atoms={\'H\',\'C\',\'N\',\'O\',\'AR\'};
        AtomicWeight=[1 12 14 16 40];
               M=0;
        for i=1:length(species)
            for j=1:length(AtomicWeight)
                if (strcmp(species(i),atoms(j))==1)
                    M=M+AtomicWeight(j);
                    g=j;
                end
            end
         % Adding the weights of the repeating atoms
                n=str2double(species(i));
                if n>1
                    p=(n-1);
                    M=M+AtomicWeight(p)*(n-1);
                end
                         
      end
end

REPORT

  1. The program uses the following commands \'fopen\' command opens the thermodynamic data file [therm.dat]and stores it to a file Id called \'f1\'. \'fgetl\' of f1 command takes the value of the first line of the therm.dat file.
  2. The 2nd line in the file represents global temperatures, thus the values are stored for temperature calculations.
  3. Since the temperatures are inside the string we use \'strsplit\' command that splits the string in the specified delimiter, here \' \' (space) is the delimiter, after splitting the values are assigned to variables ( global low, mid and high temperatures), using \'str2double\' command string value is converted to the numeric value.
  4. A temperature range with 500 values between global high and low temperature is made by using \'linspace\' command, the number of values is specified by \'linspace\' command.
  5. Preallocations are made to store the properties or data of all the elements present in the file.
  6. Then skipping the 3 comment lines present in the therm.dat file, which has command \'fgetl of f1\'.
  7. Command \'cd(pwd)\' opens a present working directory. \'mkdir\' command makes a new folder with the name specified inside brackets.
  8. A folder named \'Elements\' is created in the working directory which is used to store all the related output files of the program.
  9. Command \'cd()\' makes the new folder as the directory. At last, a \'for\' loop must run \'j\' times, where j is a number of elements.
  10. After the loop gets executed, cd .. is used to get back to the original directory.

GRAPHS FOR O2

N2

CO2

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.