Project 1 - Parsing NASA thermodynamic data
AIM:-
To Write a code in MATLAB to parse the NASA thermodynamic data file and then calculate thermodynamic properties of various gas species.
OBJECTIVE:-
- To 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.
- To Calculate the molecular weight of each species and display it in the command window.
- To 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.
• To Plots for 02, N2 and Co2 species.
INTRODUCTION:-
FILE 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 formal grammar. The term parsing comes from Latin pars (orationis), meaning part (of speech). Within computational linguistics the term is used to refer to the formal analysis by a computer of a sentence or other string of words into its constituents, resulting in a parse tree showing their syntactic relation to each other, which may also contain semantic and other information. Some parsing algorithms may generate a parse forest or list of parse trees for a syntactically ambiguous input.
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 performing some kind of transformation on the data
NASA Thermodynamic File:
NASA thermodynamics file which is THERMO.dat file in which there are temperature range and 53 species are given in these data and each species contains 14 coefficients and temperature ranges and we have to parse these 14 coefficients to find Specific heat, Entropy and Enthalpy using formula as:
where,
R = Universal Gas constant
T = Temperature
Cp = Specific heat (KJ)
S = Entropy(KJ/mol-K)
H = Enthalpy(KJ/mol)
a(i) = Coefficients where, i = 1 to 14
SPECIFIC HEAT:
The specific heat is the amount of heat per unit mass required to raise the temperature by one degree Celsius. The relationship between heat and temperature change is usually expressed in the form shown below where c is the specific heat.
Cp =R* (a1 + (a2*T) + (a3*(T).^2) + (a4*(T).^3) + (a5*(T).^4))
ENTROPY:
Entropy, the measure of a system's thermal energy per unit temperature that is unavailable for doing useful work. Because work is obtained from ordered molecular motion, the amount of entropy is also a measure of the molecular disorder, or randomness, of a system.
S=R* (a1.*log(T) + (a2*T) + ((a3* (T).^2)/2) + ((a4*(T).^3)/3) + ((a5*(T).^4)/4) + a7)
ENTHALPY:
Enthalpy, a property of a thermodynamic system, is equal to the system's internal energy plus the product of its pressure and volume. In a system contained so as to prevent mass transfer, for processes at constant pressure, the heat absorbed or released equals the change in enthalpy.
H=R*T.*(a1 + ((a2*T)/2) + ((a3*(T).^2)/3) + ((a4*(T).^3)/4) + ((a5*(T).^4)/5)+((a6./T)))
MAIN PROGRAM:-
% NASA Thermodynamic data and calculating the molecular weight
clear all
clear all
clc
% Opening the data from the folder
f1=fopen('THERMO.DAT','r');
% getting the header data
fgetl(f1);
% getting the global temp data
header_global_temp=fgetl(f1);
% split the global string data
A=strsplit(header_global_temp,' ');
% string data is converted into number and used for evaluation
global_low_temp=str2num(A{2});
global_mid_temp=str2num(A{3});
global_high_temp=str2num(A{4});
% Reading header theoritical data
for i=1:3
fgetl(f1);
end
% reading all line data
for i=1:53
tline1=fgetl(f1)
tline2=fgetl(f1);
tline3=fgetl(f1);
tline4=fgetl(f1);
% Finding string for tline_2
loc_temp=strsplit(tline1,' ');
species_name=(loc_temp{1});
loc_low_temp=str2num(loc_temp{end-3});
loc_mid_temp=str2num(loc_temp{end-2});
loc_high_temp=str2num(loc_temp{end-1});
% finding the strings line 2
% Finding the co_efficients
% finded string lines converted into numbers
a=findstr(tline2,'E');
a1=tline2(1:a(1)+3);
a1=str2num(a1);
a2=tline2(a(1)+4:a(2)+3);
a2=str2num(a2);
a3=tline2(a(2)+4:a(3)+3);
a3=str2num(a3);
a4=tline2(a(3)+4:a(4)+3);
a4=str2num(a4);
a5=tline2(a(4)+4:a(5)+3);
a5=str2num(a5);
% finding the strings line 3
% Finding the co_efficients
% finded string lines converted into numbers
a=findstr(tline3,'E');
a6=tline3(1:a(1)+3);
a6=str2num(a6);
a7=tline3(a(1)+4:a(2)+3);
a7=str2num(a7);
a8=tline3(a(2)+4:a(3)+3);
a8=str2num(a8);
a9=tline3(a(3)+4:a(4)+3);
a9=str2num(a9);
a10=tline3(a(4)+4:a(5)+3);
a10=str2num(a10);
% finding the strings line 4
% Finding the co_efficients
% finded string lines converted into numbers
a=strfind(tline4,'E');
a11=tline4(1:a(1)+3);
a11=str2num(a11);
a12=tline4(a(1)+4:a(2)+3);
a12=str2num(a12);
a13=tline4(a(2)+4:a(3)+3);
a13=str2num(a13);
a14=tline4(a(3)+4:a(4)+3);
a14=str2num(a14);
% subsuite the values of a1 to a14 to calculate the enthalpy,entropy & specific_heat
% Applying the universal gas constant
R=8.134;
T=linspace(loc_low_temp,loc_high_temp,300);
% calling the function's of cp,H,S
cp=specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,R,T,loc_mid_temp);
H=enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,loc_mid_temp,T);
S=entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,loc_mid_temp,T);
% plotting
% creating a folder 'SPECIES' to save all the species_name
mkdir('C:\Users\Vishnu kiran\Downloads',species_name)
% plotting Cp vs T
figure(1);
plot(cp,T,'linewidth',3,'Color','r');
title('specific_Heat vs Temperature',species_name);
xlabel('Specific_Heat [j/k]');
ylabel('Temperature [k]');
saveas(1,'cp vs T.jpeg');
grid on
% plotting H vs T
figure(2);
plot(H,T,'linewidth',3,'Color','b');
title('Enthalpy vs Temperature',species_name);
xlabel('Enthalpy [j]');
ylabel('Temperature [k]');
saveas(2,'H vs T.jpeg');
grid on
% plotting S vs T
figure(3);
plot(S,T,'LineWidth',3,'Color','g');
title('Entropy vs Temperature',species_name);
xlabel('Entropy [kj-1/mol-1]');
ylabel('Temperature [k]');
saveas(3,'S vs T.jpeg');
cd ('C:\Users\Vishnu kiran\Downloads') ;
grid on
molecular_weight= mol_weight(species_name)
end
CODE EXPLANATION:-
Main Program:
(1) First the Thermo.dat file is opened to read using fopen command and is saved in a specific file identifier.
(2) Now the second line is extracted using fgetl command. (3) This second line is splitted using strsplit command and then its each entry is accessed and converted into numeric value using str2num command. This will provide global temperature range.
(4) Next three lines are skipped, as they are only part of header, using textscan command.
(5) Now we have the data that needs to be operated but before starting, we create a folder using mkdir command at a specific location and name as new_folder.
(6) Now, we need to extract data from every next four lines till it reaches END. So, we use for loop which will break when it reach to the work 'END'. For this purpose, we use strcmpi command which return logical 1 when it reaches the word we are seeking for and will eventually break the loop.
(7) After entering into the loop, we access lines one by one to collect the data.
(8) The first line will provide local temperature range in the same way as we have found global temperature range.
(9) In second line, we find where 'E word appears using strfind command. It help to find the value of coefficient. This line have five coefficients written in scientific notation. So, the value of coefficient will be all number before 'E' and three position after E. The next coefficient will start from where the first ends.
(10) The value extracted is in string format so we convert it into numeric values using str2num command.
(11) Same logic is used for next two lines, only difference is that the fourth lines have only four coefficients, therefore, changes are made accordingly.
(12) Ones we have all the coefficients, we can calculate values of specific heat, enthalpy and entropy against various local temperature. Since the value of coefficients are different according to the temperature value, if condition is used.
(13) Three figures are created using figure and plot command. They are labelled using xlabel, ylabel and title command.
(14) A subfolder in new_folder is created with the name of component using mkdir command with giving the location under new folder and name the subfolder as that of component using sprintf command.
(15) A new subfolder is created each time loop run and their corresponding three figures are dumped into it using saveas command providing the location of subfolder with name of that component.
(16) After that molecular weight is calculated by calling the function defined seperately and the result is printed Command window
FUNCTION PROGRAM:-
1. SPECIFIC HEAT
%function to find specific heat:
function [cp]=specific_heat(al,a2,a3,a4,a5,a6,a7,a8,a9,a10,R,T,loc_mid_temp)
if T>=loc_mid_temp
cp=(al+a2.*(T) +a3.* (T.^2)+a4.* (T.^3)+a5.* (T.^4))*R;
else
[cp]=(a6+a7.*(T)+a8.* (T.^2)+a9.* (T.^3)+a10.* (T.^4))*R;
end
end
STEPS :
• Now we will find the Specific heat by creating Specific heat function and assigning its relted formula coefficent from 14 coefficients, Gas constant, Temperature and mid temperature.
• Now we will creatre an condition using if loop and make the temperature less than and equal to mid temperature to specify lower temperature coefficent in the formula.
• Then we will use else condition to specify coefficient of Higher temperature coefficient in the formula.
• Then end the loop and function to Calculate Specific Heat
2. ENTROPY
%function code to find the entropy
function[S] = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,loc_mid_temp,T)
R = 8.314;
if (T>loc_mid_temp)
S = R*(a1*logT)+(a2*T)+((a3*(T.^2))/2)+((a4*(T.^3)/3)+(a5*(T.^4)/4)+a7);
else
S = R*((a8*log(T)+(a9*T)+(a10*(T.^2)/2)+(a11*(T.^3))/3)+(a12*(T.^4)/4)+a14);
end
STEPS
• Now we will find the Entropy by creating Entropy function and assigning its relted formula coefficent from 14 coefficients, Gas constant, Temperature and mid temperature.
• Now we will creatre an condition using if loop and make the temperature less than and equal to mid temperature to specify lower temperature coefficent in the formula.
• Then we will use else condition to specify coefficient of Higher temperature coefficent in the formula.
• Then end the loop and function to calculate Entropy.
3.ENTHALPY
%Function program for enthalpy
function[H] = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,loc_mid_temp,T)
R = 8.314;
if (T>loc_mid_temp)
H = ((a1+((a2*T)/2)+(a3*(T.^2))/3)+((a4*(T.^3))/4)+((a5*(T.^4))/5)+(a6./T)).*T*R;
else
H = ((a8+((a9*T)/2)+((a10*T.^2))/3)+((a11*(T.^3))/4)+((a12*(T.^4))/5)+(a13./T)).*T*R;
end
STEPS :
• Now we will find the Enthalpy by creating Enthalpy function and assigning its relted formula coefficent from 14 coefficients, Gas constant, Temperature and mid temperature.
• Now we will creatre an condition using if loop and make the temperature less than and equal to mid temperature to specify lower temperature coefficent in the formula.
• Then we will use else condition to specify coefficient of Higher temperature coefficent in the formula.
• Then end the loop and function to calculate Enthalpy.
4. MOLECULAR WEIGHT
function [w]=mol_weight(species_name)
atomic_names=['H', 'C', 'O', 'N', 'A'];
atomic_weights=[1.00784,12.0107,15.999,14.0067,39.948];
w=0;
for i=1:length(species_name)
for j=1:length(atomic_names)
if strcmpi(species_name(i), atomic_names (j))
w=w+atomic_weights(j);
pos=j;
end
end
n=str2double(species_name(i));
if n>1
w =w+atomic_weights(pos)*(n-1);
end
end
fprintf('Molecular weight of %s = %f', species_name, w);
end
STEPS -
Firstly we create an Molecular_weight function W and assigning species input to that.
• Then define the main element of all 53 species in an array atomic_name that are 'H','C','0','N', 'A'.
• Then by taking above main elements we will create their atomic_wieght array from any source like the internet that are:- 1.00784,12.0107,15.999,14.0067,39.948
• Then we will assign molecular starting range W To zero and then create an for loop condition which runs to the length of the species and an another nested for loop is created which runs to the length of the string atomic_name.
• Then we will create an condition using if condition and use strcmp command to compare the species and atomic_name strings to calculate molecular masses if by comparison both string found same then we will assign its molecular weight.
• Now, we will string to the number using str2double command assign to n. Then we run an condition of if loop to find weather the species contains more than one element if there are more then I elements that is n>1 then we will increment the atomic weight by the multiplication of position and n-1 to the atomic_weight array.
• Then we will print the Molecular weight using fprintf command.
RESULTS:-
Output
Screenshot of the Directory where different species were saved

Output of molecular weight of different species:











PLOTS:-
1.PLOT FOR O2:



2.PLOT FOR N2:



3. PLOT FOR CO2:



ERROR:

- for this above shown error which i have made was corrected by placing end command in suitable place to end for 'for' loop correctly in function code for molecular weight then the program runned succesfully.
CONCLUSIONS:-
In this project report, the theory and application of the File Parsing were discussed for reading the THERMO.dat' file in MATLAB. Therefore, the following are the conclusions that can be made on the basis of the study done on the topic:
1. The use of File Parsing in this report shows that we can read all kinds of desired data from files that contain them and utilize these data for any analysis that has to be done.
2. We can read data from any line, at any position in the file as long as we use the correct strategy to extract the same.
3. By using File Parsing, we can analyze any data file with good efficiency as well as save valuable time while doing so.