Challenge

Project 1 - Parsing NASA thermodynamic data

AIM:

1. 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(Tip: use fopen and fgetl). The formulae are shown below.

Here R is the universal gas constant, T is the temeprature

The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients.

Also use the LOCAL TEMPERATURE RANGES which is unique for each individual species, not the GLOBAL TEMPERATURE RANGE.

2. Calculate the molecular weight of each species and display it in the command window.

3. Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.

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

5. The main program that you will submit will just take the "THERMO.dat" file as an input and generate Cp, H and S plots.

6. Please write a detailed report to describe your logic.

Logic to extract data from 'Thermo.dat' file:

  • It can be seen that the 1st line is the title and the 3rd, 4th and 5th lines are just comments, which are not in the scope of our parsing(they dont possess any valuble data needed for our calculation).
  • The 2nd line has 3 temperature values which are the global temperatures for the entire data set. This can be stored in a variable and also not significant for our calculation as we intend to use the local temperatures of each species in particular to plot and calculate.
  • From the 6th line the species data starts and goes upto the end of the dataset. It can easily be noted a significant pattern in the species data distribution.
  • That is, each species has 4 lines of data.
    1. The 1st species line has, the local low, High and mid temperatures respectively.
    2. Whereas, the 2nd to 4th species line has the 14 coefiicient values(unique for each species).
    3. The first 7 coefficients are high temp coeffs, ie. the temp value above mid temperature must be used in order to calcualte Cp, H and S.
    4. The last 7 coefficients are low temp coeffs, ie. the temp values below mid temperature must be used in order to calculate Cp, H and S.

Introduction:

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. The term parsing comes from Latin pars (orationis), meaning part (of speech).

Parsing in computer languages refers to syntactic analysis of the input code into its components parts in order to facilitate the writing of compilers and interpreters. It can be used to describe split or separation.

Parsing a file means reading in a data stream of some sort and building an in memory model of the semantic content of that data. This aims at facilitating perfoming some kind of transformation on the data.

Types of parsers

The task of the parser is essentially to determine if and how the input can be derived from the start symbol of the grammar. This can be done in essentially two ways:

Top-down parsing - Top-down parsing can be viewed as an attempt to find left-most derivations of an input-stream by searching for parse trees using a top-down expansion of the given formal grammar rules. Tokens are consumed from left to right. Inclusive choice is used to accommodate ambiguity by expanding all alternative right-hand-sides of grammar rules.[11] This is known as the primordial soup approach. Very similar to sentence diagramming, primordial soup breaks down the constituencies of sentences.[12]

Bottom-up parsing - A parser can start with the input and attempt to rewrite it to the start symbol. Intuitively, the parser attempts to locate the most basic elements, then the elements containing these, and so on. LR parsers are examples of bottom-up parsers. Another term used for this type of parser is Shift-Reduce parsing.

SOLUTION:

Programming Language: MATLAB

Code :-

Function Code -

function M = molecular_weight(species_name)
species = (species_name);
elements = ['H','C','N','O','A'];
Atomic_weight = [1,12,14,16,40]; % Assigning atomic weight for each array element of a species
M=0; % Initialising value for M
for i=1:length(species)
for j = 1:length(elements)
if strcmp(species_name(i),elements(j)) %Compares the species name with species array
M=M+Atomic_weight(j);
x=j;
end
end
n=str2num(species_name(i)); % extracts the numbers present in the species name
if n>1
M=M+(Atomic_weight(x)*(n-1)); % To add the repetitive elements of the species with the weight
end
end
end

Main Code -

close
clear
clc
file = fopen('THERMO.dat','r'); %Opening the NASA thermo data file and reading the data
%Reading the Header data
for f=1:5
fgetl(file);
end
%Extracting individual Species information
for i=1:53 % total 53 individual species
species= fgetl(file);
T1 = strfind(species,'.');
temp1 = species(T1(1)-3:T1(1)+3);
t_min = str2double(temp1); % local %% local low temoerature
temp2 = species(T1(2)-4:T1(2)+3);
t_max = str2double(temp2); % local high temperature
temp3 = species(T1(3)-4:T1(3)+3);
t_mid = str2double(temp3); % local mid temperature
species_name = species(1:6); % Longest species has 6 Alphabets
Line_1 = fgetl(file); % Getting the first line of species
%Extracting the constants c1 to c5 from 1st species line using "E" as indicator
L1 = strfind(Line_1,'E');
c1 = str2num(Line_1(1:(L1(1)+3)));
c2 = str2num(Line_1((L1(1)+4):(L1(2)+3)));
c3 = str2num(Line_1((L1(2)+4):(L1(3)+3)));
c4 = str2num(Line_1((L1(3)+4):(L1(4)+3)));
c5 = str2num(Line_1((L1(4)+4):(L1(5)+3)));
Line_2 = fgetl(file);
%Extracting the constants c6 to c10 from 2nd species line using "E" as indicator
L2 = strfind(Line_2,'E');
c6 = str2num(Line_2(1:(L2(1)+3)));
c7 = str2num(Line_2((L2(1)+4):(L2(2)+3)));
c8 = str2num(Line_2((L2(2)+4):(L2(3)+3)));
c9 = str2num(Line_2((L2(3)+4):(L2(4)+3)));
c10 = str2num(Line_2((L2(4)+4):(L2(5)+3)));
Line_3 = fgetl(file);
%Extracting the constants c11 to c14 from 3rd species line using "E" as indicator
L3 = strfind(Line_3,'E');
c11 = str2num(Line_3(1:(L3(1)+3)));
c12 = str2num(Line_3((L3(1)+4):(L3(2)+3)));
c13 = str2num(Line_3((L3(2)+4):(L3(3)+3)));
c14 = str2num(Line_3((L3(3)+4):(L3(4)+3)));
% To calculate Cp, H, and S
T=linspace(t_min,t_max,500); %Temperature
R=8.314; %Universal gas constant in J/mol-K
for j=1:length(T)
if T>t_mid
cp = (c1+c2.*T+c3.*T.^2+c4.*T.^3+c5.*T.^4).*R;
EH = (c1+(c2.*T)./2+(c3.*T.^2)/3+(c4.*T.^3)/4+(c5.*T.^4/5)+c6./T).*T.*R;
En = (c1.*log(T)+c2.*T+(c3.*T.^2)/2+(c4.*T.^3)/3+(c5.*T.^4)/4+c7).*R;
else T<=t_mid;
cp = (c8+c9.*T+c10.*T.^2+c11.*T.^3+c12.*T.^4).*R;
EH = (c8+(c9.*T)./2+(c10.*T.^2)/3+(c11.*T.^3)/4+(c12.*T.^4/5)+c13./T).*T.*R;
En = (c8.*log(T)+c9.*T+(c10.*T.^2)/2+(c11.*T.^3)/3+(c12.*T.^4)/4+c14).*R;
end
end
cd('C:\Users\Anish\Desktop\anish\skill lync\Matlab\nasa file parsing')
mkdir(species_name)
cd(species_name)
figure(1)
plot(T,EH)
title('Temperature vs Enthalpy')
xlabel('Temperature')
ylabel('Enthalpy')
saveas(figure(1),'T vs EH.png')
figure(2)
plot(T,En)
title('Temperature vs Entropy')
xlabel('Temperature')
ylabel('Entropy')
saveas(figure(2),'T vs En.png')
figure(3)
plot(T,cp)
title('Temperature vs Specific Heat')
xlabel('Temperature')
ylabel('Specific Heat')
saveas(figure(3),'T vs cp.png')
% Molecular weight calculation
cd('C:\Users\Anish\Desktop\anish\skill lync\Matlab');
molecular_W = molecular_weight(species_name);
K = sprintf('Molecular weight of %s is %f',species_name,molecular_W);
disp(K) % The weight is made to display in the command window
end
Result -
Species O
Species O2
Species H
Species H2
Species OH
Species H2O
Species HO2
Species H2O2
Species C
Species CH
Species CH2
Species CH2(S)
Species CH3
Species CH4
Species CO
Species CO2
Species HCO
Species CH2O
Species CH2OH
Species CH3O
Species CH3OH
Species C2H
Species C2H2
Species C2H3
Species C2H4
Species C2H5
Species C2H6
Species CH2CO
Species HCCO
Species HCCOH
Species H2CN
Species HCN
Species HNO
Species N
Species NNH
Species N2O
Species NH
Species NH2
Species NH3
Species NO
Species NO2
Species HCNO
Species HOCN
Species NCO
Species CN
Species HCNN
Species N2
Species AR
Species C3H8
Species C3H7
Species CH3CHO
Species CH2CHO

CODE EXPLAINATION:-

FUNCTION CODE EXPLAINATION-

  • First step is to assign the value for each element using an array.
  • Then using 'strcmp' command the species name and array elements are compared. Then the atomic weight is added according to the compared string element value.
  • To find the repetiton of elements of the same species, the numerical value(n) is extracted using the str2 num and then the atomic weight of the previous element is added to 'n-1' times.

MAIN CODE EXPLAINATION-

Before writing the code, it's important to start with clear all, close all and clc commands since

Clear all - removes data from the workspace, freeing up system memory

Close all- deletes the current figure or the specified figure

clc - clears all input and output from the Command Window display, giving you a "clean screen

  1. The first step is to open the data file using the fopen command and the 'r' denotes reading the file.
  2. Using fgetl command the first line data is taken and stored in a variable line_1.
  3. Again using the fgetl command results in reading the next consecutive line in the data file. Thus using this command 5 times we can extarct the first five line by the use of the FOR loop.
  4. There are totally 53 species as explained below,
    • total lines = 218.
    • irrelevant lines = 6 (first 5 lines as explained above and the last line which has 'END').
    • total relevant species lines = 218-6 = 212.
    • each species has 4 lines of data.
    • so, number of species = 212/4 = 53 species.
  5. Using a 'for' loop with the 53 species, and with fgetl command each line of species is read and the coefficient values are stored from c1 to c14.
  6. The coeff values can be found using the 'E' as an indicator and with strfind command.
  7. Using a nested 'for' loop the Cp, H and S values are calculated as explained above.
  8. Now the extracted data is saved in a directory with the species name using the 'mkdir' command.
  9. The graphs for each species are plotted and the images are saved in the directory using the saveas command.
  10. To calculate the molecular weight a separate function is used with name 'molecular_weight'.
  11. The value of universal gas constant is taken as 8.314 J/mol-K.
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.