Project 1 - Parsing NASA thermodynamic data
PARSING NASA THERMODYNAMIC DATA
AIM - 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.
OBJECTIVE -
- To extract all the coefficients of the species mentioned in the data file and calculate their parameters like enthalpy, entropy, and specific heat.
- To plot the variation of these parameters for the specified local temperature range
- To determine the molecular masses of the individual species mentioned in the data file.
RELATED THEORY :-
Parsing is the process of breaking a data block into smaller chunks by following a set of rules, so that it can be more easily interpreted, managed, or transmitted by a computer. NASA came up with polynomials that can be used to evaluate thermodynamic properties such as Cp, H and S using polynomials. They have also documented the co-efficients that are required to evaluate these polynomials for 1000+ species.
FORMULAE
EXPLANATION
1.) First, we Initially open the given thermodynamic data file and read the data in the file and store the data to the variable F.
2.) Then we read the header file of thermodynamic data and calculate the global low,mid and high temperature from the second line of header file.
3.) By using fgetl(), strsplit() and str2num() command, we find out the global low,mid and high temperatures in the second line.
4.) Then we calculate the 14 coefficients for all the species in the given thermodynamic data file.
5.) In data file each species has three lines in which 14 coefficients are there.
6.) By using the for loop, we calculate the 14 coefficients for all the species.
7.) By using strfind() command, we find out the number of exponential in first line, by which we can find out the number of coefficient in first line.
8.) Once you find out, then convert the coefficient from string form to number form by using str2num () command.
9.) By doing the above steps for line two and three, we can calculate the 14 coefficients for all the species.
10.) Once you find out the coefficients for a species, then calculate the entropy, enthalpy and specific heat.
11.) First 7 coefficients are high temperature coefficients and calculate cp,H and s.
12.) Assuming universal gas constant R=8.314 J/mol-k and assuming temperature range from 100 to 5000 take 100 values by linspace() command.
13.) Once you calculate the entropy, enthalpy and specific heat for all the species, plot the graph between: Temperature vs Entropy, Temperature vs Enthalpy and Temperature vs Specific heat.
14.) Store all the three plots for all the species in the file. The stored file is shown in the above picture.
15.) Finally calculating the molecular weight for all the species and displayed in the command window.
16.) The method for calculating molecular weight is shown in the function program.
MAIN CODE
%==>Parsing NASA Thermodynamic data<==%
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_mid_temp =str2double(C{3});
global_high_temp =str2double(C{4});
%---------------------------------------------------------------------------------
%skipping 3 comment lines
fgetl(f1);
fgetl(f1);
fgetl(f1);
%------------------------------------------------------------------------------------
%using for loop to consider 53 elements
for i = 1:53
%Reading the line
line= fgetl(f1);
species=line(1:6)
species=upper(species);
%Seperating Variable
A=strsplit(line,' ');
%Naming First Variable as 'Species'
species=A(1);
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);
ht = str2double(local_high_temp);
mt = str2double(local_mid_temp);
%------------------------------------------------------------------------------------
%Reading coefficients line-1
cfline1= fgetl(f1);
X = strfind(cfline1,'E');
%1st coefficient
a1 = cfline1(1:X(1)+3);
a1 = str2double(a1);
%2nd coefficient
a2 = cfline1(X(1)+4:X(2)+3);
a2 = str2double(a2);
%3rd coefficient
a3 = cfline1(X(2)+4:X(3)+3);
a3 = str2double(a3);
%4th coefficient
a4 = cfline1(X(3)+4:X(4)+3);
a4 = str2double(a4);
%5th coefficient
a5 = cfline1(X(4)+4:X(5)+3);
a5 = str2double(a5);
%-----------------------------------------------------------------------------------
%Reading coefficients line-2
cfline2 = fgetl(f1);
G = strfind(cfline2,'E');
%6th coefficient
a6 = cfline2(1:G(1)+3);
a6 = str2double(a6);
%7th coefficient
a7 = cfline2(G(1)+4:G(2)+3);
a7 = str2double(a7);
%8th coefficient
a8 = cfline2(G(2)+4:G(3)+3);
a8 = str2double(a8);
%9th coefficient
a9 = cfline2(G(3)+4:G(4)+3);
a9 = str2double(a9);
%10th coefficient
a10 = cfline2(G(4)+4:G(5)+3);
a10 = str2double(a10);
%---------------------------------------------------------------------------------
%Reading coefficients line-3
cfline3 = fgetl(f1);
I = strfind(cfline3,'E');
%11th coefficient
a11 = cfline3(1:I(1)+3);
a11 = str2double(a11);
%12th coefficient
a12 = cfline3(I(1)+4:I(2)+3);
a12 = str2double(a12);
%13th coefficient
a13 = cfline3(I(2)+4:I(3)+3);
a13 = str2double(a13);
%14th coefficient
a14 = cfline3(I(3)+4:I(4)+3);
a14=str2double(a14); %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 folders
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
saveas(figure(1), 'cp.png');
saveas(figure(2), 'h.png');
saveas(figure(3), 's.png');
cd(cur_dir);
%---------------------------------------------------------------------------------
%Molecular mass
M= molecular_mass(species)
end
%---------------------------------------------------------------------------------
fclose(f1);
%----------------------------------------------------------------------------------
CODE FOR SPECIFIC HEAT
%function for specific heat
function Cp=specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt)
if T>=mt
Cp=(a1+a2.*(T)+a3.*(T.^2)+a4.*(T.^3)+a5.*(T.^4))*R;
else
Cp=(a8+a9.*(T)+a10.*(T.^2)+a11.*(T.^3)+a12.*(T.^4))*R;
end
CODE FOR ENTHALPY
%function for enthalpy
function H=enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt)
if T>=mt
H=(a1.*T+a2.*(T.^2)./2+a3.*(T.^3)./3+a4.*(T.^4)./4+a5.*(T.^5)./5+a6)*R;
else
H=(a8.*T+a9.*(T.^2)./2+a10.*(T.^3)./3+a11.*(T.^4)./4+a12.*(T.^5)./5+a13)*R;
end
CODE FOR ENTROPY
%function for entropy
function S=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt)
if T>=mt
S=(a1.*log(T)+a2.*T+a3.*(T.^2)./2+a4.*(T.^3)./3+a5.*(T.^4)./4+a7)*R;
else
S=(a8.*log(T)+a9.*T+a10.*(T.^2)./2+a11.*(T.^3)./3+a12.*(T.^4)./4+a14)*R;
end
CODE FOR MOLECULAR MASS
%Function_for_Molecular_Weight%
function M= molecular_mass(species)
atoms={'H','C','N','O','A'};
AtomicWeight=[1.00794 12.0107 14.0067 15.9994 39.948];
M=0;
for i=1:length(species)
for j=1:length(atoms)
if strcmp(species(i),atoms(j))
M=M+AtomicWeight(j);
g=j;
end
end
% Adding the weights of the repeating atoms
n=str2double(species(i));
if n>1
M=M+(AtomicWeight(g)*(n-1));
end
end
end
ERRORS – WRONG SYNTAX DURING LOADING THE FILE
ERROR RECTIFIED BY USING THE RIGHT SYNTAX FOR READING THE FILE.
OUTPUT –
1. FOLDER SHOWING ALL FILES
2. MOLECULAR MASS IN COMMAND WINDOW
3. FOR O2
FOR N2
FOR CO2
CONCLUSION- HENCE THE CODE TO extract the 14 co-efficients TO calculate the enthalpy, entropy and specific heats for all the species in the data file ARE WRITTEN AND RUN SUCCESFULLY .
REFERENCES
https://www.engineeringtoolbox.com/carbon-dioxide-d_974.html