A PROGRAM PARSING NASA THERMODYNAMIC DATA
AIM:-
To write a code in MATLAB to parse the NASA thermodynamic data file and then calculate thermodynamic properties, MOLECULAR WEIGHT and Plot nature of the thermodynamic properties of various species.
OBJECTIVES:-
- To write a function that extracts the 14 CO-EFFICIENT from the given species.
Cp/R = a1 + a2 T + a3 T^2 + a4 T^3 + a5 T^4
H/RT = a1 + a2 T /2 + a3 T^2 /3 + a4 T^3 /4 + a5 T^4 /5 + a6/T
S/R = a1 lnT + a2 T + a3 T^2 /2 + a4 T^3 /3 + a5 T^4 /4 + a7
Here R is the universal gas constant, T is the temeprature
- To write a function that calculate the Enthalpy, Entropy and Specific Heat using the NASA polynomials for the given species.
- To write a function that plot the Cp, Enthalpy nd Entropy of an input species.
- To write a function that calculate the Molecular weight of each species.
INTROUCTION:-
File Parsing 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. 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.
In the following program we are using NASA Polynomial formate for CHEMKIN-II. The Chemkin \'THERMO.dat\' file formate is based very closely on the NASA(1971) file formate.
- First, in the program the value of the Universal Gas Constant R = 8.314
- The given data is the formatted as a \"THERMO.dat\" file.
- Use the \"fopen\" command the file is used to open the file in the read mode specified by \'r\' keyword in the fopen command.
- the file is read line by line using the \"fgetl\" command. The first line represents the title if the file do it is ignored.
- The next line has the global maximum, global minimum and global mid values which is read by using \'fgetl\' command and split into three values by using the \'strsplit\' command and converted into numbers.
- The temperature array is defined using \'linspace\' command as 500 values between the global minimum and global maximum values.
- The next three lines are comment in the file so they are ignored by using \'for loop\' to iterate for 3 values.
- Then \'for loo\' is created to lool through the 53 species data given in the file and calculated the required values.
- Inside the loop the \'fgetl\' command is used to read the next line of the file which represent the name of the species.
- The name of the next element is stored in a variable using \'strtok\' command.
- The next three lines of the file is read which contains the high and low temperature data of the particular species.
- The read lines are in the form of a string so they are converted into number using the \'str2double\' command and stored in specific variables so they can be used for further calculation.
- The low temperature values are stored in variables a1,a2,a3,a4,a5,a6 & a7 and the high temperature values are stored in variables b1,b2,b3,b4.b5,b6 & b7.
- The next line is the reference to a function called specific heat which calculate and returns the value of Enthalpy using the high and low temperature valyes as inputs.
- The next line is the reference to a function called Entropy which calculate and returs the value of Entropy using the high and low temperature values as inputs.
- The next line is the reference to a function that plot graphs using the given inputs.
- The next line is the reference to a function that calculates the Molecular Weights of the different element.
- The NASA Thermodynamic data file is closed using the \'fclose\' command.
FUNCTION TO CALCULATE THE ENTHALPY:-
This function uses the high temperature co-efficients, low temperature co-efficients, universal gas constant and global minimum, global mid and global maximum temperature values as the input parameters.
Then using the \'if else\' conditional statement the temp values is compared with the global mid temperature values and the appropriate equation with the proper maximum or minimum temperature co-efficient is selected and the Enthalpy is calculated and its value represented by \'H\' is returned.
% FUNCTION TO CALCULATE ENTHALPY
function H = enthalpy(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,...temp,global_min,global_max,global_mid)
if temp<=global_mid
H = R.*temp.*(a1 + a2.*(temp/2) + a3.*((temp.^2)./3) + a4.*((temp.^3)./4) + a5.*((temp.^4)./5) + a6./temp);
else
H= R.*temp.*(b1 + b2.*(temp./2) + b3.*((temp.^2./3) + b4.*((temp.^3)./4) + b5.*((temp.^4)./5) + b6./temp);
end
FUNCTION TO CALCULATE THE SPECIFIC HEAT:-
This function uses the high temperature co-efficients, low temperature co-efficients, universal gas constant and global minimum, global mid and global maximum temperature values as the input parameters.
Then we use the \'if else\' conditional statement to check if the temperature values are less than or equal to the global mid temperature values and select the appropriate equations with high and low temperature co-efficients is used to calculate the specific heat value denoted by \'c_p\' and return that value.
% FUNCTION TO CALCULATE SPECIFIC HEAT
function c_p = specific_heat(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,...temp,global_min,global_max,global_mid)
if temp<=global_mid
c_p = R*(a1 + a2.*temp + a3.*(temp.^2) + a4.*(temp.^3) + a5.*(temp.^4));
else
c_p = R*(b1 + b2.*temp + b3.*(temp.^2) + b4.*(temp.^3) + b5.*(temp.^4));
end
FUNCTION TO CALCULATE THE ENTROPY:-
This function uses the high temperature co-efficients, low temperature co-efficients, universal gas constant and global minimum, global mid and global maximum temperature values as the input parameters.
Then we use the \'if else\' conditional statement the temperature values are compared with the global mid temperature values and the appropriate equation with the proper maximum or minimum temperature co-efficients is selected and the Entropy is calculated and its value represented by \'S\' is returned.
% FUNCTION TO CALCULATE ENTROPY
function H = enthalpy(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,temp,global_min,global_max,global_mid)
if temp<=global_mid
S = R*(a1.*log(temp) + a2.*temp + a3.*(temp.^2)./2 + a4.*(temp.^3)./3 + a5.*(temp.^4)./4 + a7);
else
S= R*(b1.log(temp) + b2.*temp + b3.*(temp.^2)./2 + b4.*(temp.^3)./3 + b5.*(temp.^4)./5 + b7);
end
FUNCTION PLOTTING TO PLOT AND SAVE THE GRAPH:-
This function has no output arguments it only uses H, c_p, S, temperature, element_name as input arguments and plots the required graphs.
The code of the function is explained below-
1. Using the \'mkdir\' command a new folder is created in the folder \'pwd\' with the same name as the element name.
2. The first figure(1) represented the plots the Temperature vs Enthalpy values.
3. The first figure(2) represented the plots the Temperature vs Specific Heat values.
4. The first figure(1) represented the plots the Temperature vs Entropy values.
5. Using the \'saveas\' command the three figure are saved in the new folder which created by the element name.
6. Using the \'cd\' command the folder is changed back to the original folder.
7. The funcion runs for 53 times through the \'for\' loop and it creates seprate folders for each species and saves all the plots in the folders.
% FUNCTION TO PLOT GRAPHS FOR ALL ELEMENTS
function plotting(c_p,H,S,temp,element_name)
cd(pwd);
mkdir(dir_name);
% PLOTTING TEMPERATURE vs ENTHALPY
figure(2)
plot(temp,H,\'linewidth\',3,\'color\',\'g\');
xlabel(\'Temperature\');
ylabel(\'Enthalpy\');
title(sprintf(\'Enthalpy vs Temperature rn %s:\',element_name));
% PLOTTING TEMPERATURE vs SPECIFIC HEAT
figure(1)
plot(temp,c_p,\'linewidth\',3,\'color\',\'r\');
xlabel(\'Temperature\');
ylabel(\'Specific Heat\');
title(sprintf(\'Specific Heat vs Temperature rn for %s:\',element_name));
% PLOTTING TEMPRETURE vs ENTROPY
figure(3)
plot(temp,S,\'linewidth\',3,\'color\',\'b\');
xlabel(\'Temperature\');
ylabel(\'Entropy\');
title(sprintf(\'Enthalpy vs Temperature rn %s:\',element_name));
cd(dir_name);
saveas(1,\'H_vs_temp.png\');
saveas(2,\'c_p_vs_temp.png\');
saveas(3,\'S_vs_temp.png\');
cd .. ;
end
FUNCTION TO CALCULATE THE MOLECULAR WEIGHT OF ALL ELEMENTS:-
This function for molecular weight calculations requires element name as input argument and it is saved in the variable element and the output argument is molecular.
1. A string array is created which constains the name of the stoms.
2. A variable atomic_wt is created which the atomic weight of respective atoms in the string array.
3. Then used \'strcmp\' command to compare the name of the element with the string array.
4. If there is a repetition of an element \'str2double\' used to find that and (n-1) is multiplied to atomic_wt of the element to calculate molecular weight.
% FUNCTION FOR CALCULATING MOLECULAR WEIGHT
function [w] = molecular_weight(element_name)
s = {\'H\' \'C\' \'N\' \'O\' \'AR\'};
% ATOMIC WEIGHT FOR EACH ELEMENTS
atomic_wt = [1 12 14 16 40];
w = 0;
for i = 1:length(element_name)
for j = 1:length(s)
if strcmp(element_name(i), s(j))
w=w + atomic_wt(j);
% STORING THE POSITION OF MATCHED ELEMENT
position = j;
end
end
n = str2double(element_name(i));
if n > 1
w = w + atomic_wt(position)*(n-1);
end
end
end
FUNCTION TO WRITE THE MOLECULAR WEIGHT DATA OF ALL ELEMENTS TO A FILE:-
The above function takes the elements name and the molecular weights as the data inputs and writes the molecular weight along with the name of the element in a file.
1. Firstly a new file is created with name \'mol_wt.dat\' and it is opened in append mode denoted by \'a+\' so that we can write the molecular weight of all elements in a single file.
2. Using the \'fprintf\' command the name of the element along with their molecular weight is written to the file.
3. The file is then closed.
% WRITING MOLECULAR WEIGHT OF ELEMENTS TO A FILE
function writing_to_file(element_name,weight)
dir_name = element_name;
mol_file = fopen(\'mol_wt.dat\',\'a+\');
fprintf(mol_file, \'molecular wieght of %s n\', dir_name,wieght);
fclose(mol_file);
end
PROGRAM:-
clear all
close all
clc
% UNIVERSAL GAS CONSTANT
R = 8.314;
% READING THE DATA FILE
f1 = fopen(\'THERMO.dat\',\'r\');
% READING THE FIRST LINE
L1 = fgetl(f1);
% GETTING THE GLOBAL MINIMUM, MAXIMUM AND MID TEMPERATURE
L2 = fgetl(f1);
t = strsplit(L2);
global_min = str2double(t{2});
global_mid = str2double(t{3});
global_max = str2double(t{4});
% DEFINING TEMPERATURE RANGE
temp = linspace(global_min,global_max,500);
for i = 1:3
fgetl(f1);
end
for i = 1:53
% REDAING THE FIRST LINE WHICH INCLUDES THE INFORMATION FOR SPECIES
l = fgetl(f1);
% READING THE NAME OF THE SPECIES
element_name = strtok(l);
% READING THE THREE LINES WHICH CONTAINS 7 HIGH TEMPERATUR CO-EFFICIENTS AND 7 LOW C0-EFFICIENTS
l1 = fgetl(f1);
l2 = fgetl(f2);
l3 = fgetl(f3);
% FINDING THE POSITION OF E SO THAT WE CAN EXTRACT THE CO-EFFICIENTS
a = strfind(l1,\'E\');
b = strfind(l2,\'E\');
c = strfind(l3,\'E\');
% EXTRACTING HIGH TEMPERATURE CO-EFFICIENTS
a1 = str2double(l1(1:a(1)+3));
a2 = str2double(l1(a(1)+3:a(2)+3));
a3 = str2double(l1(a(2)+4:a(3)+3));
a4 = str2double(l1(a(3)+4:a(4)+3));
a5 = str2double(l1(a(4)+4:a(5)+3));
a6 = str2double(l1(1:b(1)+3));
a7 = str2double(l2(b(1)+4:b(2)+3));
% EXTRACTING LOW TEMPERATURE CO-EFFICIENTS
b1 = str2double(l2(b(2)+4:b(3)+3));
b2 = str2double(l2(b(3)+4:b(4)+3));
b3 = str2double(l2(b(4)+4:b(5)+3));
b4 = str2double(l3(1:c(1)+3));
b5 = str2double(l3(c(1)+4:c(2)+3));
b6 = str2double(l3(c(2)+4:c(3)+3));
b7 = str2double(l3(c(3)+4:c(4)+3));
% CALCULATING ENTHALPY
H = enthalpy(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,temp,global_min,global_max,global_mid);
% CALCUALTING SPECIFIC HEAT
c_p = specific_heat(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,temp,global_min,global_max,global_mid);
% CALCULATING ENTROPY
S = entropy(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,temp,global_min,global_max,global_mid);
% PLOTTING GRAPHS
plotting(H,c_p,S,temp,element_name);
% CALCULATING MOLECULAR WEIGHTS
mol_wt(i) = molecular_weight(element_name);
disp(mol_wt(i));
Molecular_weight = strcat(mol_wt(i),element_name)
% FUNCTION TO WRITE MOLECULAR WEIGHT TO A FILE
weight = mol_wt(i);
writing_to_file(element_name,weight);
end
fclose(f1)
ERROR & SOLUTION:-
* I did mistake while writing program i am forgot writing complete function or variables after that i did correct. L1 = fgetl(f1)
CONCLUSION:-
1. SCREENSHOT OF ALL THE FOLDER ARE SAVED:-
1. ALL PLOTS OF N2:-
2. ALL PLOTS OF O2:-
3. ALL PLOTS OF CO2:-