Challenge

Project 1 - Parsing NASA thermodynamic data

AIM: -

  • To write a code in Matlab parse the NASA Thermodynamic data file and then calculate thermodynamic properties of various gas species.

THEORY: -

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 denes algorithmic procedures for components. These work to form an expression and dene 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

Scripting languages

Protocols, such as HTTP and Internet remote function calls.

TERMINOLOGY:

  • PARSING: Parsing, syntax analysis, or syntactic analysis is the process of analyzing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal grammar.
  • SPECIFIC HEAT: Specific heat is the amount of heat energy required to raise the temperature of a body per unit of mass Specific heat is also known as specific heat capacity or mass specific heat. ln SI units, specific heat (symbol: c) is the amount of heat in joules required to raise temperature of 1 gram of a substance by 1 Kelvin.
  • 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.
  • ENTROPV: The entropy of an object is a measure of the amount of energy which is unavailable to do work. Entropy is also a measure of the number of possible arrangements the atoms in a system can have. In this sense, entropy is a measure of uncertainty or randomness.
  • GAS CONSTANT: The gas constant is a physical constant denoted by R and is expressed in terms of units of energy per temperature increment per mole. The gas constant value is equivalent to Boltzmann constant but expressed as the pressure-volume product instead of energy per increment of temperature per particle.
  • MOLECULAR WEIGHT: Molecular weight is a measure of the sum of the atomic weight values of the atoms in a molecule. Molecular weight is used in chemistry to determine stoichiometry in chemical reactions and equations. Molecular weight is either unitless or expressed in terms of atomic mass units.

FORMULAE:

PROCEDURE:

1.) Open the given thermodynamic data file and read the data in the file and store the data to the variable file.

2.) Then read the header file of thermodynamic data and calculate the global low, mid and high temperature from the second line of the header file.

3.) By using fgetl(), strsplit() and str2num() command we find out the global low, mid and high temperatures in the second line.

4.) Then we calculate the 14 coefficients for all the species in the given thermodynamic data file.

5.) In data file each species has three lines in which 14 coefficients are there.

6.) By using the for loop, we calculate the 14 coefficients for all the species.

7.) By using strnd() command, we find out the number of exponential in rest line, by which we can find out the number of coefficient in first line.

8.) Once we find out, then we convert the coefficient from string form to number form by using str2num() command.

9.) By doing the above steps for line two and three, we can calculate the 14 coefficients for all the species.

10.) Once we find out the coefficients for a species, then we calculate the entropy, enthalpy and specific heat.

11.) First 7 coefficients are high temperature coefficients and calculate Cp, H and s.

12.) Assuming universal gas constant R=8.314 J/mol-k and assuming temperature range from 100 to 5000 take 200 values by linspace() command.

13.) Once we calculate the entropy, enthalpy and specific heat for all the species, we plot the graph between: Temperature vs Entropy, Temperature vs Enthalpy and Temperature vs Specific heat.

14.) Finally, we calculate the molecular weight for all the species and displayed in the command window.

15.) The method for calculating molecular weight is shown in the function program.

PLOT:

  • N2

  • CO2

  • O2

Folder carrying graph of various species:

Molecular weight calculated simultaneously in command window

CODE:

clear all
close all
clc

R = 8.314;

file = fopen('THERMO.dat','r');
Header= fgetl(file);

Global_temp = fgetl(file);
C = strsplit(Global_temp,' ');
Global_low_temp =str2double(C{2}); 
Global_mid_temp =str2double(C{3}); 
Global_high_temp =str2double(C{4}); 


fgetl(file);
fgetl(file);
fgetl(file);

for i = 1:53
	line= fgetl(file); 
	A=strsplit(line,' '); 
	Species=A(1); 
	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); 
	HT = str2double(local_high_temp); 
	MT = str2double(local_mid_temp); 

	
	
	Cfline1= fgetl(file);
	X = strfind(Cfline1,'E');
	A1 = Cfline1(1:X(1)+3);
	A1 = str2double(A1); 
	A2 = Cfline1(X(1)+4:X(2)+3);
	A2 = str2double(A2);
	A3 = Cfline1(X(2)+4:X(3)+3);
	A3 = str2double(A3); 
	A4 = Cfline1(X(3)+4:X(4)+3);
	A4 = str2double(A4);
	A5 = Cfline1(X(4)+4:X(5)+3);
	A5 = str2double(A5); 

	Cfline2 = fgetl(file);
	G = strfind(Cfline2,'E');
	A6 = Cfline2(1:G(1)+3);
	A6 = str2double(A6); 
	A7 = Cfline2(G(1)+4:G(2)+3);
	A7 = str2double(A7); 
	A8 = Cfline2(G(2)+4:G(3)+3);	
	A8 = str2double(A8); 
	A9 = Cfline2(G(3)+4:G(4)+3);
	A9 = str2double(A9); 
	A10 = Cfline2(G(4)+4:G(5)+3);
	A10 = str2double(A10);

	Cfline3 = fgetl(file);
	I = strfind(Cfline3,'E');
	A11 = Cfline3(1:I(1)+3);
	A11 = str2double(A11);
	A12 = Cfline3(I(1)+4:I(2)+3);
	A12 = str2double(A12); 
	A13 = Cfline3(I(2)+4:I(3)+3);
	A13 = str2double(A13); 
	A14 = Cfline3(I(3)+4:I(4)+3);	
	A14=str2double(A14); 

	T = linspace(LT,HT,200);
	
	Cp = Specific_Heat(A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14,T,R,LT,HT,MT);

	S = Entropy(A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14,T,R,LT,HT,MT);

	H = Enthalpy(A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14,T,R,LT,HT,MT);
	
	spec = char(Species);
	fd = sprintf('%s',spec);
	cur_dir = pwd;
	mkdir(spec);
	cd(spec);

	figure(1);
	plot(T,Cp,'linewidth',2,'color','r');
	xlabel('Temperature in K');
	ylabel('Specific heat in kJ');
	title('Specific heat vs Temperature');

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

	saveas(figure(1),'cp.png');
	saveas(figure(2),'h.png');
	saveas(figure(3),'s.png');
	cd(cur_dir);

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

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

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

Molecular Mass:

function M = Molecular_Mass(Species)
    Atoms = {'H','C','N','O','AR'};
    Atomic_Weight=[1 12 14 16 40];
    M=0;
    for i = 1:length(Species)
        for j = 1:length(Atomic_Weight)
            if (strcmp(Species(i),Atoms(j))==1)
                M = M + Atomic_Weight(j);
                g = j;
            end
        end
 
        n = str2double(Species(i));
        if n>1
            p =(n-1);
            M = M + Atomic_Weight(p)*(n-1);
        end

    end
end

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
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.