NASA thermodynamic file parsing
AIM : write a code in Matlab to parse the NASA thermodynamic data file and then calculate thermodynamic properties of various gas species.
OBJECTIVE :
- code to parse the NASA thermodynamic data file and calculate the thermodynamic properties of various gas species.
- a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.
- Calculate the molecular weight of each species and display it in the command window.
- Plot the Cp, Enthalpy and Entropy for the local temperature range 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.
- Screenshot of the window where all the folders are saved.
- Plots for o2, N2 and Co2 species .
THEORY :
NASA came up with polynomials that can be used to evaluate thermodynamic properties such as Cp, H and S. They have also documented the co-efficients that are required to evaluate these polynomials for 1000+ species.
the formula using for the calculation of specific heat (Cp), enthalpy(H) and entropy(s) are as follows :
here ;
- a1,a2,a3,a4,a5,a6,a7 are the coefficients
- T is the temperature in kelvin
- R is universal gas constant
- Cp is specified for specific heat
- H is specified for the enthalpy
- S is specifed for the entropy
REQUIRED INPUTS :
the inputs are taken from the file from NASA thermodynamic file which is saved as \'THERMO\'. the first line is taken as title or headline and from the very first sixth line we are taking the inputs as the local temperature varients as local_low_temp , local_mid_temp and local_high-temp.
taking R (universal gas contant value) as R = 8.3145 in j/mol.k
and for the calculation of molecular weights the atomic weight of the following atoms are taken as input , in these pogram we are taking in consider the following atoms :
[\'H\',\'C\',\'O\',\'N\',\'R\',\'S\'] which atomic weights are [1 12 16 14 40 32]
MATLAB CODE :
the following main code for the pogram is
clear all
close all
clc
f1 = fopen(\'THERMO.dat\',\'r\')
%skipping the first lline
skippinglines = fgetl(f1)
tline = fgetl(f1)
A = strsplit (tline,\' \')
global_low_temp = str2num(A{2})
global_mid_temp = str2num(A{3})
global_high_temp = str2num(A{4})
%skipping the next three lines
for i = 1:3
skkippinglines = fgetl(f1)
end
% create for loop to calculate all species data
for i= 1:53
% taking the local temperature
line1 = fgetl(f1)
[name,local_temp] = strtok(line1,\'G\')
species_name = strtok(name)
temp = strsplit(local_temp)
local_low_temp = str2double(temp{2})
local_mid_temp = str2double(temp{3})
local_high_temp = str2double(temp{4})
%defining the temperature range from local temperature limits
T = linspace(local_low_temp,local_high_temp,800)
% taking data of species
tline_1 = fgetl(f1)
tline_2 = fgetl(f1)
tline_3 = fgetl(f1)
a = findstr(tline_1,\'E\')
b = findstr(tline_2,\'E\')
c = findstr(tline_3,\'E\')
% here A values are denoted for high temperature coefficient
% here B values are denoted for low temperature coefficient
a1 = tline_1(1:a(1)+3)
A1 = str2num(a1)
a2 = tline_1(a(1)+4:a(2)+3)
A2 = str2num(a2)
a3 = tline_1(a(2)+4:a(3)+3)
A3 = str2num(a3)
a4 = tline_1(a(3)+4:a(4)+3)
A4 = str2num(a4)
a5 = tline_1(a(4)+4:a(5)+3)
A5 = str2num(a5)
b1 = tline_2(1:b(1)+3)
A6 = str2num(b1)
b2 = tline_2(b(1)+4:b(2)+3)
A7 = str2num(b2)
b3 = tline_2(b(2)+4:b(3)+3)
B1 = str2num(b3)
b4 = tline_2(b(3)+4:b(4)+3)
B2 = str2num(b4)
b5 = tline_2(b(4)+4:b(5)+3)
B3 = str2num(b5)
c1 = tline_3(1:c(1)+3)
B4 = str2num(c1)
c2 = tline_3(c(1)+4:c(2)+3)
B5 = str2num(c2)
c3 = tline_3(c(2)+4:c(3)+3)
B6 = str2num(c3)
c4 = tline_3(c(3)+4:c(4)+3)
B7 = str2num(c4)
% defining the value of universal gas constant in (J/mol.k)
R = 8.3145
% calculating specific heat
Cp = specific_heat(A1,A2,A3,A4,A5,B1,B2,B3,B4,B5,R,T,local_mid_temp)
% calculating the enthalpy
H = enthalpy(A1,A2,A3,A4,A5,A6,B1,B2,B3,B4,B5,B6,R,T,local_mid_temp)
% calculating the entropy
S = entropy(A1,A2,A3,A4,A5,A7,B1,B2,B3,B4,B5,B7,R,T,local_mid_temp)
% calculating the molecular weight of following atoms
atoms = [\'H\',\'C\',\'O\',\'N\',\'R\',\'S\']
atomic_weight = [ 1 12 16 14 40 32 ]
MW = 0
for i = 1:length(species_name)
for j = 1:length(atoms)
if strcmp(species_name(i),atoms(j))
MW=MW+atomic_weight(j);
dt = j;
end
end
n= str2double(species_name(i))
if n>1
MW=MW+(atomic_weight(dt).*(n-1));
end
end
fprintf(\'molecular weight of %s:%f\',species_name,MW)
disp(\' \')
% plotting the outputs
% creating the file directory to store plots
mkdir(species_name)
cd(species_name)
% plot for specific heat
figure(1)
plot(T,Cp,\'color\',\'r\')
title(\'specific heat values\')
grid on
xlabel(\'temperature\')
ylabel(\'specific heat\')
saveas(gcf,\'temperature vs specific heat .png\')
% plot for enthalpy
figure(2)
plot(T,H,\'color\',\'r\')
title(\'enthalpy value\')
grid on
xlabel(\'temperature\')
ylabel(\'enthalpy\')
saveas(gcf,\'temperature vs enthalpy .png\')
% plot for entropy
figure(3)
plot(T,S,\'color\',\'r\')
title(\'entropy value\')
grid on
xlabel(\'temperature\')
ylabel(\'entropy\')
saveas(gcf,\'temperature vs entropy .png\')
cd(pwd)
cd ..
end
the code for the following functions used in the main pogram are as follows:
1) function for specific heat
% function for specific heat
function[Cp] = specific_heat(A1,A2,A3,A4,A5,B1,B2,B3,B4,B5,R,T,local_mid_temp)
if T>local_mid_temp
Cp = (A1+(A2*T)+(A3*(T.^2))+(A4*(T.^3))+(A5*(T.^4))).*R
else
Cp = (B1+(B2*T)+(B3*(T.^2))+(B4*(T.^3))+(B5*(T.^4))).*R
end
2) function for the enthalpy
% pogram for function of enthalpy
function[H]= enthalpy(A1,A2,A3,A4,A5,A6,B1,B2,B3,B4,B5,B6,R,T,local_mid_temp)
if T>local_mid_temp
H = R.*T.*(A1+(A2.*T)./2+(A3.*T.^2)./3+(A4.*T.^3)./4+(A5.*T.^4)./5+A6./T);
else
H = R.*T.*(B1+(B2.*T)./2+(B3.*T.^2)./3+(B4.*T.^3)./4+(B5.*T.^4)./5+B6./T);
end
end
3) fucntion for the entropy
% pogram for function of entropy
function[S] = entropy(A1,A2,A3,A4,A5,A7,B1,B2,B3,B4,B5,B7,R,T,local_mid_temp)
if T>local_mid_temp
S = R.*(A1.*log(T) + A2.*T + (A3.*T.^2)./2 + (A4.*T.^3)./3+ (A5.*T.^4)./4+ A7)
else
S = R.*(B1.*log(T) + B2.*T + (B3.*T.^2)./2 + (B4.*T.^3)./3+ (B5.*T.^4)./4+ B7)
end
end
ERRORS AND EXPLAINATION :
error 1:
here in the function pogram of specific heat we get an error due to the \'.\' dot product in formula
error 2 :
here in the error the function which is saved for specific heat is different from the call of function variable in the main function. the function saved as \'cp\' and in the pogram we are calling it with \'Cp\'.
which is corrcted in the pogram.
error 3 :
here in the function we had error issue with the division factor and the brackets assigned to the variables.
error 4 :
here we came up with the issue of using log of temperature varients
by the help of the support engineer and the popups of error showed up in the command window the errors are corrected
EXPLAINING THE CODE :
here in the pogram first we read the following file \'THERMO.dat\' and then skip the first line and then taken the global temperature varients and then skip the comments line and after that we are taken the local temp varients
now we taken the all species variables in loop and taken the local temperature varients in consider for the following temperature difference
now we are taking the variables of the species in consider for the following pogram
similarly we are taking all the variables of the following species in consider for the pogram , in these pogram here we are differentiating the \'A\' coefficients as the above local_mid_temp and the \'B\'
coefficients as the below local_mid_temp coefficients as per the requirement .
now we are calculating the specific heat , enthalpy and entropy by calling up the function and then calculate the molecular weights of the following species which are required
now we use command for plotting and create a folder to save the values or plot values of the following species which are given in the following \'THERMO.dat\' file
simiarly we plot for the enthalpy and entropy also and then by help of loop we save different values plots of different species in the current folder and then end the pogram
RESULTS :
hence, the parsing of the following NASA thermodynamic file is done and the following values are taken as output as per the concern the molecular weights of the following species are shown in the command window.
the following plots are saved in the \'pc folder\' of the user and the screenshot of the saved plots folders of different species are given below
the following plots of O2 , N2 and CO2 are given below
dioxygen (O2) :
plot for temperature vs specific heat
plot for the temperature vs enthalpy
plot for the temperature vs entropy
Nitrogen (N2) :
plot for the temperature vs specific heat
plot for the temperature vs enthalpy
plot for the temperature vs entropy
Carbondioxide(CO2) :
plot for the temperature vs specific heat
plot for the temperature vs enthalpy
plot for the temperature vs entropy
REFERENCES :
- https://in.mathworks.com/matlabcentral/fileexchange/26139-molecular-weight-calculator-function
- https://in.mathworks.com/help/matlab/ref/strtok.html?s_tid=srchtitle
- https://in.mathworks.com/matlabcentral/answers/52243-why-str2double-is-preferred-over-str2num
- http://matlab.izmiran.ru/help/techdoc/ref/mkdir.html
- https://in.mathworks.com/help/matlab/ref/saveas.html?searchHighlight=save%20gcf&s_tid=doc_srchtitle
- https://in.mathworks.com/help/matlab/ref/cd.html
- support engineer \' D AJAY\'