Project 1 - Parsing NASA thermodynamic data
Aim:
Writing a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.
Objectives:
- Calculate the molecular weight of each species and display it in the command window.
- Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species. Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.
- 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.
- The main program that you will submit will just take the "THERMO.dat" file as an input and generate Cp, H and S plots.
Theory:
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 process of "parsing" can be described as reading in a data stream of some sort and building an in-memory model or representation of the semantic content of that data, in order to facilitate performing some kind of transformation on the data.
- Read the entire file into a character string.
- Split the string into a cell array of strings, with one block in each cell
- Pre-allocate output variables based on the size of the string and the cell array
- Loop over all blocks and parse one block at a time
Parser 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. Parsing is of two types: top down parsing and bottom up parsing.
The File Parser process enables you to convert data from an external file into PeopleSoft data and place it into tables in your Campus Solutions database. The external file can be a delimited file or a flat file. It can be a simple file with one row type or a complex file with several row types. If a institution routinely receives an external file from which you need to move data into your database, consider using the File Parser process to expedite the entry of that data. Whether the file is a small one that your institution receives daily or a large one that is received annually, the one-time setup of the File Parser process for that file can save significant data entry time.
To set up and use the File Parser process for an external file:
- Set up a field conversion definition that identifies a field values to convert from the external file to the target staging table.
- Set up context definitions that identify how to convert the data and in which staging tables to place the converted data to hold it for processing. Each context must also identify the parent-to-child relationship of the staging tables.
- Set up a file mapping definition that associates fields on the external file with the context definition to use.
- View the layout of each staging table, modify it, and map the fields from it to the target tables.
- Preview the hierarchical record layout and the converted data before you move the data into the staging tables.
- Run the File Parser process to convert the data and place it into the staging tables.
Code For the calculation and file parcing:
clear all
close all
clc
f1=fopen('THERMO.dat','r') %reading the document
line1=fgetl(f1)
% To read the Header Lines
header=fgetl(f1)
%Spliting the string in place of space
A=strsplit(header,' ');
%Getting the global temperature values
g_low_temp=str2double(A{2}) %Low temperature
g_mid_temp=str2double(A{3}) %Mid temperature
g_high_temp=str2double(A{4}) %High temperature
%Getting the 3 Header line
for i=1:3
fgetl(f1)
end
for i = 1:53
line= fgetl(f1); %Reading the line
elements = line(1:6)
A=strsplit(line,' '); %Seperating Variable
variables=A(1); %Naming First Variable as 'Species'
X = strfind(line,'G');
low_temp = line(X+3:X+10);
high_temp = line(X+13:X+20);
mid_temp = line(X+23:X+32);
ltemp = str2double(low_temp); %Local Low Temperature
mtemp = str2double(mid_temp); %Local mid Temperature
htemp = str2double(high_temp); %Local High Temperature
line2= fgetl(f1);
B = strfind(line2,'E');
b1 = line2(1:B(1)+3);
b1 = str2double(b1);
b2 = line2(B(1)+4:B(2)+3);
b2 = str2double(b2);
b3 = line2(B(2)+4:B(3)+3);
b3 = str2double(b3);
b4 = line2(B(3)+4:B(4)+3);
b4 = str2double(b4);
b5 = line2(B(4)+4:B(5)+3);
b5 = str2double(b5);
line3 = fgetl(f1);
C = strfind(line3,'E');
c1 = line3(1:C(1)+3);
c1 = str2double(c1);
c2 = line3(C(1)+4:C(2)+3);
c2 = str2double(c2);
c3 =line3(C(2)+4:C(3)+3);
c3 = str2double(c3);
c4 = line3(C(3)+4:C(4)+3);
c4 = str2double(c4);
c5 = line3(C(4)+4:C(5)+3);
c5 = str2double(c5);
line4 = fgetl(f1);
D = strfind(line4,'E');
d1 =line4(1:D(1)+3);
d1 = str2double(d1);
d2 = line4(D(1)+4:D(2)+3);
d2 = str2double(d2);
d3 = line4(D(2)+4:D(3)+3);
d3 = str2double(d3);
d4 = line4(D(3)+4:D(4)+3);
d4=str2double(d4);
mkdir('X:species',elements)
R=8.314;
T=linspace(ltemp,htemp,500);
%Specific Heat
Cp=specific_heat(b1,b2,b3,b4,b5,c1,c2,c3,c4,c5,d1,d2,d3,d4,T,R,ltemp,htemp,mtemp);
%Entropy
S=entropy(b1,b2,b3,b4,b5,c1,c2,c3,c4,c5,d1,d2,d3,d4,T,R,ltemp,htemp,mtemp);
%Enthaphy
H=enthalpy(b1,b2,b3,b4,b5,c1,c2,c3,c4,c5,d1,d2,d3,d4,T,R,ltemp,htemp,mtemp);
%Molecular mass
M= molecular_mass(variables);
%Plot 1, Specific heat (Cp) vs Temperature (T)
figure(1);
plot(T,Cp,'linewidth',2,'color','g');
title(elements);
xlabel('Temperature in K');
ylabel('Specific heat in kJ');
%Plot 2, Enthalpy (H) vs Temperature (T)
figure(2);
plot(T,H,'linewidth',2,'color','b');
title(elements);
xlabel('Temperature in K');
ylabel('Enthalpy in kJ/(mol)');
%Plot 3, Entropy (S) vs Temperature (T)
figure(3);
plot(T,S,'linewidth',2,'color','r');
title(elements);
xlabel('Temperature in K');
ylabel('Entropy in kJ/(mol K)')
saveas(1,'specific.jpg')
saveas(2,'enthalphy.jpg')
saveas(3,'entripy.jpg')
end
fclose(f1);
The function for the calculation of Specific heat:
function Cp=specific_heat(b1,b2,b3,b4,b5,c1,c2,c3,c4,c5,d1,d2,d3,d4,T,R,ltemp,htemp,mtemp)
if T>=mtemp
Cp=(b1+b2.*(T)+b3.*(T.^2)+b4.*(T.^3)+b5.*(T.^4))*R;
else
Cp=(c3+c4.*(T)+c5.*(T.^2)+d1.*(T.^3)+d2.*(T.^4))*R;
end
The function for the calculation of the Enthalpy:
function H=enthalpy(b1,b2,b3,b4,b5,c1,c2,c3,c4,c5,d1,d2,d3,d4,T,R,ltemp,htemp,mtemp)
if T>=mtemp
H=(b1.*T+b2.*(T.^2)./2+b3.*(T.^3)./3+b4.*(T.^4)./4+b5.*(T.^5)./5+c1)*R;
else
H=(c3.*T+c4.*(T.^2)./2+c5.*(T.^3)./3+d1.*(T.^4)./4+d2.*(T.^5)./5+d3)*R;
end
The function for the calculation of the Entropy:
function S=entropy(b1,b2,b3,b4,b5,c1,c2,c3,c4,c5,d1,d2,d3,d4,T,R,ltemp,htemp,mtemp)
if T>=mtemp
S=(b1.*log(T)+b2.*T+b3.*(T.^2)./2+b4.*(T.^3)./3+b5.*(T.^4)./4+c2)*R;
else
S=(c3.*log(T)+c4.*T+c5.*(T.^2)./2+d1.*(T.^3)./3+d2.*(T.^4)./4+d4)*R;
end
The function for the calculation of Molecular weight:
function w= molecular_mass(variables);
atoms={'H','C','N','O','AR'};
AtomicWeight=[1 12 14 16 40];
w=0;
for i=1:length(variables);
for j=1:length(AtomicWeight) ;
if (strcmp(variables(i),atoms(j))==1) ;
w=w+AtomicWeight(j);
g=j;
end
end
k=str2double(variables(i));
if k>1
p=(k-1);
w=w+AtomicWeight(p)*(k-1);
end
end
The output:
The step-wise explaination of the codes:
- By using the fopen command the file is stored into a variable f1. The file THERMO.dat is readed using this command.
- The command fgetl is used to read the string line by line.This command generally returns the string value..
- The header is readed using the fgetl(f1) command . For spliting the data in required format a specific charecter is noted and from their data is splited.
- Here the spliting is done when their is space is present. The format used here is A=strsplit(header,' ')
- The global low, midium and high temperatures are specifically located and stored into the variables like,
g_low_temp=str2double(A{2}) %Low temperature
g_mid_temp=str2double(A{3}) %Mid temperature
g_high_temp=str2double(A{4}) %High temperature - The string is converted into the number values by using the str2double, so that it is easier for the calculation purposes. The str2double converts string to floating number.
- For further reading the data the for loop is used and this is split and stored into the variable. The variable 'x' is assigned to locate the position of 'G', with respect to the position of 'G' the position of local tempereture is located. Like,
low_temp = line(X+3:X+10);
high_temp = line(X+13:X+20);
mid_temp = line(X+23:X+32); - For reading the data from the second, third and forth line the variable 'B' is used to locate the position of the 'E' , and with respect to this position the values of the b1, b2, b3, b4, b5 c1, c2, c3, c4, c5, d1, d2, d3 and d4 is assigned.
- The value obtained is of string so this is converted to proper format using str2double.
line2= fgetl(f1);
B = strfind(line2,'E');
b1 = line2(1:B(1)+3);
b1 = str2double(b1);
b2 = line2(B(1)+4:B(2)+3);
b2 = str2double(b2); - Specific heat, enthapy, entropy and molecular mass is calculated using the different functions and formulea.
- The plot is obtained for different data values.
%Plot 1, Specific heat (Cp) vs Temperature (T)
%Plot 2, Enthalpy (H) vs Temperature (T)
%Plot 3, Entropy (S) vs Temperature (T)
The plots obtained:
The creation of directory and storage of plots in this directory:
Conclusion:
By file parsing method we can read the data from the different files and we can use thiis data for different perposes.
References: