Project 1 - Parsing NASA thermodynamic data
AIM
To write a matlab code to parsing thermodynamic data file and calculate the various properties of different gas species
OBJECTIVE
- Write a function to extract 14coefficients and calculate enthalpy entropy, specific heat and molecular weight for all species
- Plot specific heat, Enthalpy, Entropy at different temperatures
- Save plot images
THEORY
- File parsing
- The term parsing is to split the file or other input into a pieces or data which can be converted into useful data, that can be stored or manipulated.
- Here we extract the data from the given thermodynamic data file and will calculate the enthalpy, entropy, molecular weight and specific heat for all species
Main code explanation
- To open data file use fopen command
- To get the header ‘THERMO’,use fgetl command for reading each line
- The secod line contains global temperatures, use fgetl to read this line
- Use strsplit command split to split the data by using empty space as the delimiter
- The three values are global min,global mid,global high temperatures, then str2double command to convert string values to numbers
- 3rd,4th,5th lines are comments,we can avoid these line by using fgetl command or by using for loop
for i=1:3
fgetl(f1);
end
- Use strfind command to find the places of E in line 7,strfind is using for find strings in strings
- Then get all the coefficients a1-a7,b1-b7(total 14)by using str2double command
Main code
clear all
close all
clc
%open the thermodynamic data
f1=fopen('THERMO.dat','r');
%skip the first line
b=fgetl(f1)
%read the next line which contains the global low,medium and high temperature
A=fgetl(f1);
% create array to store the global temperature values
A=strsplit(A,' ');
%convert string value into numerical data
global_low_temp=str2num(A{2});
%convert string value into numerical data
global_mid_temp=str2num(A{3});
%convert string value into numerical data
global_high_temp=str2num(A{4});
%skip the next three lines which is irreleavant for the analysis purpose
for i=1:3
fgetl(f1);
end
%%commands to store the coefficients of temperature, local temperatures and
%%calculate the specific heat, enthalpy and entropy
for i=1:53
% read the line that contains the species name and local temperatures
header=fgetl(f1);
% split the character datatype into cell array containing each word in diferent cell, seperated by a space
x=strsplit(header,' ');
%first array of x is stored with name of species for which the characterisctic curves are obtained
species_name=(x{1});
%calculate the number of cells in array x
z=length(x);
% third last value of x array contains the local low temperatures and is converted into floating datatype
local_low_temp=str2double(x{z-3});
% second last value of x array contains the local high temperatures and is converted into floating datatype
local_high_temp=str2double(x{z-2});
% last value of z array contains the local medium temperatures and is converted into floating datatype
local_mid_temp=str2double(x{z-1});
%local low temperatures is stored into temporary variable t1
t1=local_low_temp;
%local high temperatures is stored into temporary variable t2
t2=local_mid_temp;
%local medium temperatures is stored into temporary variable t3
t3=local_high_temp;
% read the first line that contains 5 coefficients
line_1=fgetl(f1);
% create and store values in the line that contains the letter 'e'
a=strfind(line_1,'E');
% store the value of 1st coefficient
a1=str2double(line_1(1:a(1)+3));
% store the value of 2nd coefficient
a2=str2double(line_1(a(1)+4:a(2)+3));
% store the value of 3rd coefficient
a3=str2double(line_1(a(2)+4:a(3)+3));
% store the value of 4th coefficient
a4=str2double(line_1(a(3)+4:a(4)+3));
% store the value of 5th coefficient
a5=str2double(line_1(a(4)+4:a(5)+3));
% read the second line that contains 5 coefficients
line_2=fgetl(f1);
% create and store values in the line that contains the letter 'e'
a_1=strfind(line_2,'E');
% store the value of 6th coefficient
a6=str2num(line_2(1:a_1(1)+3));
% store the value of 7th coefficient
a7=str2double(line_2(a_1(1)+4:a_1(2)+3));
% store the value of85th coefficient
a8=str2double(line_2(a_1(2)+4:a_1(3)+3));
% store the value of 9th coefficient
a9=str2double(line_2(a_1(3)+4:a_1(4)+3));
% store the value of 10th coefficient
a10=str2double(line_2(a_1(4)+4:a_1(5)+3));
% read the third line that contains 4 coefficients
line_3=fgetl(f1);
% create and store values in the line that contains the letter 'e'
a_2=strfind(line_1,'E');
%STORE THE VALUE OF 11TH COEFFICIENT
a11=str2double(line_3(1:a_2(1)+3));
%STORE THE VALUE OF 12TH COEFFICIENT
a12=str2double(line_3(a_2(1)+4:a_1(2)+3));
%STORE THE VALUE OF 13TH COEFFICIENT
a13=str2double(line_3(a_2(2)+4:a_1(3)+3));
%STORE THE VALUE OF 14TH COEFFICIENT
a14=str2double(line_3(a_2(3)+4:a_1(4)+3));
%create a directory to store the species name
mkdir(species_name);
% create an array to store the values of temperture in between local low temperature and local high temperature
% initialising the value of temperature
T2=t1:2:t2;
T1=t2:2:t3;
R=8.314;
%if t(k)>t2% if the temperature is geater than the medium local temperature, coefficents a1,a2,a3,a4,a5,a6 and a7 are used to calculate the specific heat, enthalpy and entropy
%calculate the value of specific heat
cp1=((a1+a2.*T1)+a3.*T1.^2+a4.*T1.^3+a5.*T1.^4)*R;
%calculate the value of enthalpy
H1=(a1+(a2.*T1)/2+(a3.*T1.^2)/3+(a4.*T1.^3)/4+(a5.*T1.^4)/5+(a6./T1))*R.*T1;
%calculate the value of entropy
S1=(a1.*log(T1)+a2.*T1+(a3.*T1.^2)/2+(a4.*T1.^3)/3+(a5.*T1.^4)/4+a7)*R;
%else% if the temperature is less than the medium local temperature, take coefficients a8,a9,a10,a11,a12,a13,a14 are used for
%calculate the value of specific heat
cp2=(a8+a9.*T2+a10.*T2.^2+a11.*T2.^3+a12.*T2.^4)*R;
%calculate the value of enthalpy
H2=(a8+(a9.*T2)/2+(a10.*T2.^2)/3+(a11.*T2.^3)/4+(a12.*T2.^4)/5+(a13./T2))*R.*T2;
%calculate the value of entropy
S2=(a8.*log(T2)+a9.*T2+(a10.*T2.^2)/2+(a11.*T2.^3)/3+(a12.*T2.^4)/4+a14)*R;
%end
%end
T=[T2 T1];
cp_calc=[cp2 cp1];
h_cal=[H2 H1];
S_calc=[S2 S1];
%create figure to plot temperature vs specific heat
figure(1);
% plot the required data
plot(T,cp_calc);
grid on ;
title(species_name);
xlabel('Temperature');
ylabel('Specific Heat');
%create figure to plot temperature vs enthalpy
figure(2);
% plot the required data
plot(T,h_cal);
grid on ;
title(species_name);
xlabel('Temperature');
ylabel('Enthalpy');
%create figure to plot temperature vs entropy
figure(3);
% plot the required data
plot(T,S_calc);
grid on ;
title(species_name);
xlabel('Temperature');
ylabel('Entropy');
%open the older names with corresponding species name
cd (species_name)
% save the figure 1 with specific heat in jpg format
saveas(1,'specific heat','jpg');
% save the figure 2 with enthalpy in jpg format
saveas(2,'enthalpy','jpg');
% save the figure 3 with entropy in jpg format
saveas(3,'entropy','jpg');
% exit the current folder location to carry forward the calculations
cd ..
mol_mass = molecular_wt(species_name);
%print the molecular weight of the corresponding species
sprintf('Molecular Weight of %s:%f u',species_name,mol_mass)
end
fclose(f1)
Function for finding the molecular weight
function tot_mol_wt = molecular_wt(species)
% input argument 'species' should be of char data type
size = length(species);
%CREATE A CHARACTER ARRAY TO STORE THE VALUE OF ELEMENTS OF SPECIES
elem=['H','C','O','N','A'];
%CREATE THE ATOMIC MASS FOR THE CORRESPONDING MATERIALS
at_wt=[1.0079 12.0107 15.999 14.0067 39.948];
mol_wt = zeros(1,size);
for i = 1:size
[num,st] = str2num(species(i));
if st == 0
for j = 1:length(elem)
if species(i) == elem(j)
mol_wt(i) = mol_wt(i) + at_wt(j);
end
end
else
mol_wt(i-1) = mol_wt(i-1)*num;
end
end
tot_mol_wt = sum(mol_wt);
end
Output -(Molecular weight)
'Molecular Weight of O:15.999000 u
'Molecular Weight of O2:31.998000 u'
'Molecular Weight of H:1.007900 u’
'Molecular Weight of H2:2.015800 u'
'Molecular Weight of OH:17.006900 u'
'Molecular Weight of H2O:18.014800 u'
'Molecular Weight of HO2:33.005900 u'
'Molecular Weight of H2O2:34.013800 u'
'Molecular Weight of C:12.010700 u'
'Molecular Weight of CH:13.018600 u'
'Molecular Weight of CH2:14.026500 u'
'Molecular Weight of CH2(S):14.026500 u'
'Molecular Weight of CH3:15.034400 u'
'Molecular Weight of CH4:16.042300 u'
'Molecular Weight of CO:28.009700 u'
'Molecular Weight of CO2:44.008700 u'
'Molecular Weight of HCO:29.017600 u'
'Molecular Weight of CH2O:30.025500 u'
'Molecular Weight of CH2OH:31.033400 u'
'Molecular Weight of CH3O:31.033400 u'
'Molecular Weight of CH3OH:32.041300 u'
'Molecular Weight of C2H:25.029300 u'
'Molecular Weight of C2H2:26.037200 u'
'Molecular Weight of C2H3:27.045100 u'
'Molecular Weight of C2H4:28.053000 u'
'Molecular Weight of C2H5:29.060900 u'
'Molecular Weight of C2H6:30.068800 u'
'Molecular Weight of CH2CO:42.036200 u'
'Molecular Weight of HCCO:41.028300 u'
'Molecular Weight of HCCOH:42.036200 u'
'Molecular Weight of H2CN:28.033200 u'
'Molecular Weight of HCN:27.025300 u'
'Molecular Weight of HNO:31.013600 u'
'Molecular Weight of N:14.006700 u'
'Molecular Weight of NNH:29.021300 u'
'Molecular Weight of N2O:44.012400 u'
'Molecular Weight of NH:15.014600 u'
'Molecular Weight of NH2:16.022500 u'
'Molecular Weight of NH3:17.030400 u'
'Molecular Weight of NO:30.005700 u'
'Molecular Weight of NO2:46.004700 u'
'Molecular Weight of HCNO:43.024300 u'
'Molecular Weight of HOCN:43.024300 u'
'Molecular Weight of HNCO:43.024300 u'
'Molecular Weight of NCO:42.016400 u'
'Molecular Weight of CN:26.017400 u'
'Molecular Weight of HCNN:41.032000 u'
'Molecular Weight of N2:28.013400 u'
'Molecular Weight of AR:39.948000 u'
'Molecular Weight of C3H8:44.095300 u'
'Molecular Weight of C3H7:43.087400 u'
'Molecular Weight of CH3CHO:44.052000 u'
'Molecular Weight of CH2CHO:43.044100 u'