Project 1 - Parsing NASA thermodynamic data
AIM:
To write a code in MATLAB to parse nasa thermodynamic data file and then calculate thermodynamic properties of various gas species.
OBJECTIVE:
- Write a function that extracts the 14 coefficients and calculates the enthalpy,entropy and specific heats for all the species in the data file.
- Calculating molecular weight of each species.
- Plotting the specific heat,enthalpy and entropy for the local temperature range.
- Saving the plots as images with appropriate names and dumping them in seperate folders for each species
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.
MATHEMATICAL EQUATION:
For specific heat
cpR=b1+b2⋅T+b3⋅T2+b4⋅T3+b5⋅T4">cpR=b1+b2⋅T+b3⋅T2+b4⋅T3+b5⋅T4cpR=b1+b2⋅T+b3⋅T2+b4⋅T3+b5⋅T4
For enthalpy
HRT=b1+b2T2+b3T23+b4⋅T34+b5⋅T45+b6T">HRT=b1+b2T2+b3T23+b4⋅T34+b5⋅T45+b6THRT=b1+b2T2+b3T23+b4⋅T34+b5⋅T45+b6T
For entropy
SR=b1⋅log(T)+b2⋅T+b3⋅T22+b4⋅T33+b5⋅T44+b7">SR=b1⋅log(T)+b2⋅T+b3⋅T22+b4⋅T33+b5⋅T44+b7SR=b1⋅log(T)+b2⋅T+b3⋅T22+b4⋅T33+b5⋅T44+b7
where
T=local temprature
b1-b7=high temperature coefficient
b8-b14=low temprature coefficient
R=Gas constant=8.314
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 [c_p]=specific_heat(b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,local_low_temp,local_mid_temp,local_high_temp,t,R)
if (t>local_mid_temp)
c_p=R*(b1+(b2.*t)+(b3.*(t.^2))+(b4.*(t.^3))+(b5.*(t.^4)));
else
c_p=R*(b8+(b9.*t)+(b10.*(t.^2))+(b11.*(t.^3))+(b12.*(t.^4)));
end
For enthalpy:
function [H]=enthalpy(b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,local_low_temp,local_mid_temp,local_high_temp,t,R)
if (t>local_mid_temp)
H=(b1+((b2.*t)/2)+((b3.*(t.^2))/3)+((b4.*(t.^3))/4)+((b5.*(t.^4))/5)+(b6./t)).*t*R;
else
H=(b8+((b9.*t)/2)+((b10.*(t.^2))/3)+((b11.*(t.^3))/4)+((b12.*(t.^4))/5)+(b13./t)).*t*R;
end
For Entropy:
function [S]=entropy(b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,local_low_temp,local_mid_temp,local_high_temp,t,R)
if (t>local_mid_temp)
S=((b1.*logt)+(b2.*t)+((b3.*(t.^2))/2)+((b4.*(t.^3))/3)+(b5.*(t.^4))+b7);
else
S=((b8.*log(t))+(b9.*t)+((b10.*(t.^2))/2)+((b11.*(t.^3))/3)+(b12.*(t.^4))+b14)*R;
end
For molecular weight:
%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
OUTPUT:
FOR O2
FOR N2
FOR CO2:
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 read file is assigned to f1 in order to get the data related to the global temperature,local temperature and the coefficients to calculte the thermodynamic properties.
- fgetl command is used to go to the the next line in the file
- strsplit is used to split the value in a line
- The split values are assaigned to vector A_G_T
- String generated is converted to double and stored in G_L_T,G_M_T and G_H_T
- Next three lines can be skipped because those are not important
- In order to generate thermodynamic properties for each element we are using for loop
- In the next line local temeratures and molecule name of specific species are extracted by using strfing command
- For the next few lines by using strfind we can extract the coefficients to calculate the thermodynamic properties
- mkdir command is used to create folder for every species
- Function for solving thermodynamic process is written.
- The the respective graph is obtained
- 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)
Directory :
ERRORS:
This error happened because of wrong equation brackets it was then corrected
Conclusion:
By file parsing method we can read the data from the different files and we can use thiis data for different perposes.The graph for N2 O2 and CO2 is plotted and respected molecular weight is found.