Challenge

Project 1 - Parsing NASA thermodynamic data

AIM: -

  • To parse NASA Thermodynamic data.
  • Extract all the coefficients of the species mentioned in the data file and calculate their parameters like enthalpy, entropy, and specific heat.
  • Plot the variation of the above parameters for the specified local temperature range.
  • Determine the molecular mass of the individual species mentioned in the data file.

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.

FORMULAE: -

Where,

Cp = specific heat capacity [kJ]

H = Enthalpy [kJ/(kmol)]

S = Entropy [KJ/mol-K]

T = Temperature

R = Universal Gas Constant

a1, a2, a3, a4, a5, a6, a7 = Temperature coefficients

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 the rest line, by which we can find out the number of coefficients in the 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 lines 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.) The 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 them in the command window.

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

CODE: -

The main code is given below:

clear all
close all
clc


% UNIVERSAL GAS CONSTANT

R = 8.314;

% READING HEADER

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

% READING GLOBAL TEMPERATURE 

Global_temp = fgetl(file);
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(file);
fgetl(file);
fgetl(file);

% USING FOR LOOP

for i = 1:53
	line= fgetl(file); %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(file);
	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(file);
	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(file);
	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,200);
	
	%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 (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(file);

The code for Specific Heat function is given below:

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

The code for Enthalpy function is given below:

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

The code for Entropy function is given below:

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

PLOTS: -

Image of the directory where all files are present:

Plots for O2 are given below:

Plots for N2 are given below:

Plots for CO2 are given below:

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.