Parsing NASA thermodynamic data
AIM:
Parsing NASA thermodynamic data by using matlab.
THEORY:
What is File Parsing?
-The term ‘parsing’ refers to MATLAB reading an m-file (script or function) and translating it into an internal code that it stores in memory. It is this ‘parsed’ file in memory that is actually run when you execute the code.
GOVERNING EQUATION:
`(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`
Where,
Cp = Specific Heat(J/mol-K)
H = Enthalpy(J/kg)
S = Entropy(J/K)
T = Temperature(K)
R = Gas constant(J/molK)
a1,a2,a3,a4,a5,a6,a7 are the numerical coefficient.
OBJECTIVE:
The objectives of the project are:
- To make 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 molecular weight of each species and display in the command window.
- To plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.
- To save the plots as images with appropriate names and dump them in separate folders for each species with suitable name.
WORKFLOW OF THE PROJECT:
- Initially we open the THERMO file and then extract the values of different lines.
- Then we extract the values of of coefficients of different species.
- Afer that we make the directory.
- Then we write the code for function to find Specific heat,enthalpy and entropy of different species and call this function in our main program.
- Then we plot the curve for different species and save them in 'jpg' format.
- Atlast we find the molecular weight of all the species and display it in command window.
The link for 'NASA THERMODYNAMIC DATA' which is used in this project is shown below:
https://drive.google.com/file/d/1AikCHDlz9s_fu8whE81qee_GD4AyYpCP/view
PROGRAMMING LANGUAGE USED - MATLAB
MAIN CODE:
%A program for parsing NASA'S Thermodynamic data
clear all
close all
clc
f1 = fopen('THERMO.dat','r'); %opening the THERMO file
file_name = fgetl(f1); %getting the file name from the first line
tline_1 = fgetl(f1); %getting the file value from the second line
A = strsplit(tline_1,' '); %splitting the string values
%getting the value of Global temperature
global_low_temp = str2num(A{2});
global_mid_temp = str2num(A{3});
global_high_temp = str2num(A{4});
%getting the comments from thermo file
for i = 1:3
Comments = fgetl(f1)
end
for j = 1:53
%getting the species name
tline_2 = fgetl(f1);
s = strsplit(tline_2,' '); %splitting the string values
species_name = s{1};
A=strfind(tline_2,'G ');
temp_values=tline_2(A+4:length(tline_2));
B=strsplit(temp_values,' ');
%Getting the value of local temperature of different species
local_low_temp=str2double(B{1});
local_high_temp=str2double(B{2});
local_mid_temp=str2double(B{3});
%Extracting coefficient from first line
line_1 = fgetl(f1);
l1 = findstr(line_1,'E');
a1 = str2num(line_1(1:(l1(1)+3)));
a2 = str2num(line_1((l1(1)+4:l1(2)+3)));
a3 = str2num(line_1((l1(2)+4:l1(3)+3)));
a4 = str2num(line_1((l1(3)+4:l1(4)+3)));
a5 = str2num(line_1((l1(4)+4:l1(5)+3)));
%Extracting coefficient from second line
line_2 = fgetl(f1);
l2 = findstr(line_2,'E');
a6 = str2num(line_2(1:(l2(1)+3)));
a7 = str2num(line_2((l2(1)+4:l2(2)+3)));
a8 = str2num(line_2((l2(2)+4:l2(3)+3)));
a9 = str2num(line_2((l2(3)+4:l2(4)+3)));
a10 = str2num(line_2((l2(4)+4:l2(5)+3)));
%Extracting coefficient from third line
line_3 = fgetl(f1);
l3 = findstr(line_3,'E');
a11 = str2num(line_3(1:(l3(1)+3)));
a12 = str2num(line_3((l3(1)+4:l3(2)+3)));
a13 = str2num(line_3((l3(2)+4:l3(3)+3)));
a14 = str2num(line_3((l3(3)+4:l3(4)+3)));
%creating folder for the species
Spe_dir=pwd;
mkdir(species_name);
cd(species_name);
T = linspace(local_low_temp,local_high_temp,100);
mt = local_mid_temp;
% calling the thermodynamic equations function 'coefficient'
[Cp H S] = coefficient(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,mt);
%Plotting the curve
%Curve between Temperature and Specific Heat
figure(1)
plot(T,Cp,'linewidth',3,'color','r');
title(species_name);
xlabel('Temperature[K]');
ylabel('Specific Heat[J/mol-K]');
grid on
%Curve between Temperature and Enthalpy
figure(2)
plot(T,H,'linewidth',3,'color','b');
title(species_name);
xlabel('Temperature[K]');
ylabel('Enthalpy[J/kg]');
grid on
%Curve between Temperature and Entropy
figure(3)
plot(T,S,'linewidth',3,'color','g');
title(species_name);
xlabel('Temperature[K]');
ylabel('Entropy[J/K]');
grid on
%Saving the plot in jpg format
saveas(figure(1),'Specific Heat.jpg');
saveas(figure(2),'Enthalpy.jpg');
saveas(figure(3),'Entropy.jpg');
cd(Spe_dir);
%Calculating the Molecular Mass
elements=['H','C','O','N','A'];
atomic_weight=[1 12 16 14 40];
mol_mass=0;
for l=1:length(species_name)
for m=1:length(atomic_weight)
if strcmp(species_name(l),elements(m))
mol_mass=mol_mass+atomic_weight(m);
position=m;
end
end
n=str2double(species_name(l));
if n>1
mol_mass=mol_mass+atomic_weight(position)*(n-1);
end
end
%printing the molecular weight of differet species in command window
fprintf('molecular_weight of %s:- ',species_name)
fprintf('%d amu \n',mol_mass)
end
fclose(f1)
FUNCTION CODE:
%Function program to calculate specific heat,enthalpy and entropy
function [Cp H S] = coefficient(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,mt)
R = 8.314; %R is Universal Gas Constant
for k = 1:length(T)
if T(k)>mt;
%Specific heat
Cp = (a1 + a2.*T + a3.*T.^2 + a4.*T.^3 + a5.*T.^4).*R;
%Enthalpy
H = (a1 + a2.*T./2 + a3.*T.^2./3 + a4.*T.^3./4 + a5.*T.^4./5 + a6./T).*R.*T;
%Entropy
S = (a1.*log(T) + a2.*T + a3.*T.^2./2 + a4.*T.^3./3 + a5.*T.^4./4 +a7).*R;
else
%Specific heat
Cp = (a8 + a9.*T + a10.*T.^2 + a11.*T.^3 + a12.*T.^4).*R;
%Enthalpy
H = (a8 + a9.*T./2 + a10.*T.^2./3 + a11.*T.^3./4 + a12.*T.^4./5 + a13./T).*R.*T;
%Entropy
S = (a8.*log(T) + a9.*T + a10.*T.^2./2 + a11.*T.^3./3 + a12.*T.^4./4 +a14).*R;
end
end
end
CODE EXPLAINATION:
- First we open the THERMO.dat file using the fopen command and then get the values of different lines by using fgetl command.
- After that we split the string values by using strsplit command and the convert that string to number by using str2num command and get the values of global temperature.
- After that by using the same procedure we obtain the species name and their respective local temperature.
- Then we extract the values of coefficient of tyhe different species.
- Then we use mkdir comand to make the new directory to save the plots .
- After that we write the code for function named coefficient which gives us the value of specific heat,enthalpy and entropy and call that function in the main program.
- Then we plot the curve for different species and save the plot as jpg format in their respective folder.
- Atlast we find the value of molecular weight of different species and print them in the command window.
OUTPUTS:
Plots for O2:
TEMPERATURE V/S SPECIFIC HEAT
TEMPERATURE V/S ENTHALPY
TEMPERATURE V/S ENTROPY
Plots for N2:
TEMPERATURE V/S SPECIFIC HEAT
TEMPERATURE V/S ENTHALPY
TEMPERATURE V/S ENTROPY
Plots for CO2:
TEMPERATURE V/S SPECIFIC HEAT
TEMPERATURE V/S ENTHALPY
TEMPERATURE V/S ENTROPY
The below screenshot shows the molecular weight of different species in the command window:
The below screenshot shows the folder of each species having plot of Temperature v/s Specific Heat,Enthalpy and Entropy saved with their respective name:
CONCLUSION:
Thus the file parsing of the NASA Thermodynamic Data was done. Also the plot of Temperature v/s Specific Heat,Enthalpy and Entropy was obtained and the plots were also saved in different folders with their respective names.Also we have calculated the molecular weight of different species and shown them in the command window.
ERRORS OBTAINED AND ITS SOLUTION:
As matlab is case sensitive therefore there were many typing errors which were rectified with clear observations.Also initially I used hold on command in plots so i was getting the plots of species in same figure which was further rectified by removing the hold on command.
REFERENCES: