Parsing NASA thermodynamic data using matlab
Code:
Program used to calculate all the desired ouput values and make the required plots.
clear all
close all
clc
% Inputs
R = 8.314; %Universal gas constant
%Reading First line of the program i.e., header
f1 = fopen('THERMO.dat','r');
Header= fgetl(f1);
%Reading Global Temperature second line of the data file
temp = fgetl(f1);
%splitting the readed whole string into three individual strings
C = strsplit(temp,' ');
%Converting the obtained global low temp string into double
global_low_temp =str2double(C{2});
%Converting the obtained global medium temp string into double
global_mid_temp =str2double(C{3});
%Converting the obtained global high temp string into double
global_high_temp =str2double(C{4});
%skipping 3 lines
fgetl(f1);
fgetl(f1);
fgetl(f1);
% For loop is used to caluculate desired values for each species
for i = 1:53
line= fgetl(f1); %Reading the 6th line of the data file
A=strsplit(line,' '); %Seperating strings
species=A(1); %Naming First Variable
Q = strfind(line,'G');
local_low_temp = line(Q+3:Q+10);
local_high_temp = line(Q+13:Q+21);
local_mid_temp = line(Q+23:Q+31);
lt = str2double(local_low_temp); %Local Low Temperature
ht = str2double(local_high_temp); %Local Mid Temperature
mt = str2double(local_mid_temp); %Local High Temperature
%line 1-Reading the first 5 coefficient data in a line
cfline1= fgetl(f1);
X = strfind(cfline1,'E');
a1 = cfline1(1:X(1)+3);
a1 = str2double(a1); %storing 1st coefficient
a2 = cfline1(X(1)+4:X(2)+3);
a2 = str2double(a2); %storing 2nd coefficient
a3 = cfline1(X(2)+4:X(3)+3);
a3 = str2double(a3); %storing 3rd coefficient
a4 = cfline1(X(3)+4:X(4)+3);
a4 = str2double(a4); %storing 4th coefficient
a5 = cfline1(X(4)+4:X(5)+3);
a5 = str2double(a5); %storing 5th coefficient
%line 2-Reading the next 5 coefficient data of 2nd line
cfline2 = fgetl(f1);
G = strfind(cfline2,'E');
a6 = cfline2(1:G(1)+3);
a6 = str2double(a6); %storing 6th coefficient
a7 = cfline2(G(1)+4:G(2)+3);
a7 = str2double(a7); %storing 7th coefficient
a8 = cfline2(G(2)+4:G(3)+3);
a8 = str2double(a8); %storing 8th coefficient
a9 = cfline2(G(3)+4:G(4)+3);
a9 = str2double(a9); %storing 9th coefficient
a10 = cfline2(G(4)+4:G(5)+3);
a10 = str2double(a10); %storing 10th coefficient
%line 3-Reading the last 4 coefficient data of 3rd line
cfline3 = fgetl(f1);
I = strfind(cfline3,'E');
a11 = cfline3(1:I(1)+3);
a11 = str2double(a11); %storing 11th coefficient
a12 = cfline3(I(1)+4:I(2)+3);
a12 = str2double(a12); %storing 12th coefficient
a13 = cfline3(I(2)+4:I(3)+3);
a13 = str2double(a13); %storing 13th coefficient
a14 = cfline3(I(3)+4:I(4)+3);
a14=str2double(a14); %storing 14th coefficient
%Ranging the temperature low temperature to high temperature
T=linspace(lt,ht,500);
%Find the Specific Heat value by calling the funtion
Cp=specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt);
%Finding the Entropy value by calling the entropy function
S=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt);
%Finding the Enthaphy value by calling the enthalpy function
H=enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt);
%Creating new folder for each species
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 that are generated above
saveas(figure(1),'cp.png');
saveas(figure(2),'h.png');
saveas(figure(3),'s.png');
cd(cur_dir);
%Finding Molecular mass of each species
M= molecular_mass(species)
f2 = fopen('molecular mass','w');
fprintf('%s n %s n %f n','species','Molecular mass',M)
fclose(f2)
end
fclose(f1)
Program to calculate entropy as function:
%Program to find the entropy of the specices
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
Program to calculate enthalpy as function:
%Program to find the enthalpy of the species
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
Proram to calculate specific heat function:
%Program to calcuclate the specific heat of the species
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
Program to calulate the moecular mass as function:
%Program to calculate the molecular mass and atomic weight of the species
function M= molecular_mass(species)
atoms={'H','C','N','O','AR'};
AtomicWeight=[1 12 14 16 40];
M=0;
for i=1:length(species)
for j=1:length(AtomicWeight)
if (strcmp(species(i),atoms(j))==1)
M=M+AtomicWeight(j);
g=j;
end
end
% repeting elements
n=str2double(species(i));
if n>1
p=(n-1);
M=M+AtomicWeight(p)*(n-1);
end
end
end
Output:
Plot for various species:
O2:
N2:
Co2:
REPORT:
Aim:
To read the data in the NASA thermodynamic data file and to calculate the enthaply, entropy, specific heat, mocular weight of all the species avaliable in the given data file.
Objective of the program:
The main objective is to parsing the NASA thermodynamic report. Need to create seprate function code to find the required values. The required values are obtained by using the below equations:
3.Body of the content:
Explannation for the main program:
Line 1-3: clears the workspace and cmd window
Line 7: assgined the value for the new variable 'R' as 8.314 which is the universal gas constant value.
Line 10: opening the "THERMO.dat" file with read permissionn and saving its data to variable f1
Line 11: Reading the first line in the data file andd saving the readed value to the variable 'Header'.
Line15: Reading the second line of the data file wherre the global temperature alues are found and these values are saved to the variable 'temp'
Line 17: seprating the obtained value in temp into seprate string values i.e, three string values are obtained.
Line 19-23: Seprated three strings values are stored into three different variables as converted into daouble value from the string value.
Line 25-27:Read and Omitting the next 3 lines as these lines are comments
Line 30: For loop begins which will ranges from 1 to 53. hence 53 type of species are found to be in the data file.
Line 31: Reading the 6the line of the data file and store its values to the v: ariable name as line.
Line32: Converting the values in the varibale line as seprate strings and save the value to variable A
Line 33: Reading the first string in the variable A as it represent the name of the species hence its storeed to variable named as species
Line34: Finding the value G in the 6th line
Line 35-37: saving the local low, mid and high temp values to new variables respectively.
Line 38-40: converting the string saved in the above lines is stored into new varaibles as double
Line 45-84: Reading the coefficient values in the each line and converting the obtiained string values into double and store them in the variable
Line 90: Using linspace cmd obatining the 500 temp values between the low and high temperature.
Line 94: Calculate the specific heat using the specific heat function.
Line 98: Calculating the entropy as function
Line 102: calculate the enthalpy value using the enthalpy function
Line 105-109: Creating ne folder name in the current direcotry for each species.
Line 114: Creating a new figure 1
Line115-118: Ploting the tempreature and specific heat values and naming the x and y axis and the title.
Line122-126: Ploting the tempreature and enthalpy values and naming the x and y axis and the title.
Line130-134: Ploting the tempreature and entropy values and naming the x and y axis and the title.
Line138-140: Saving the plots in the current directory
Line 143-146: Calculating the molecular mass of the species
Line 151: end the loop
Line 152: close the file f1
In each calling function program the desired values calculated using the given equation.
Conclusion:
Thus the required values are calculated and the desiredvalues are plotted.