Parsing NASA thermodynamic data
What is File parsing?
Parsing means a method of taking input from a string and returning some other data type or vice versa.
Objective
1. To write a function that extracts the 14 coefficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.
2. Calculate the molecular weight of each species and display it in the command window.
3. Plot the Cp, Enthalpy, and Entropy for the local temperature range (low temperature: high temperature) specific for each species.
4. Save the plots as images with appropriate names and dump them in separate folders for each species with a suitable name that should be generated automatically.
5. The main program should take the \"THERMO.dat\" file as an input and generate Cp, H and S, plots for all species that are included in the file.
Logic
1. Read the document using Matlab
2. Eliminate the lines that are commented
3. Pick the number of spices that you will be taking
4. Now separate the high temp and low temp coeff.
5. Find Enthalpy Entropy and Specific heats
6. Calculate Molecular weights
7. Make a plot of the 3 found properties
8. Create and save the plots
Code
clear all
close all
clc
R = 8.3145
f1=fopen(\'THERMO.dat\',\'r\')
line1=fgetl(f1);
line2=fgetl(f1);
t=strsplit(line2);
global_low_temp=str2num(t{2});
global_mid_temp=str2num(t{3});
global_high_temp=str2num(t{4});
temp=linspace(global_low_temp,global_high_temp,400);
for i = 1:3
line1=fgetl(f1);
end
for i = 1:53
line1=fgetl(f1);
A=strsplit(line1,\' \');
speciesN=(A{1})
t1=fgetl(f1) ;
a=strfind(t1,\'E\');
% High temp co-effs
ht1=t1(1:a(1)+3);
ht1=str2num(ht1) ; %s1
ht2=t1(a(1)+4:a(2)+3);
ht2=str2num(ht2) ; %s2
ht3=t1(a(2)+4:a(3)+3);
ht3=str2num(ht3) ; %s3
ht4=t1(a(3)+4:a(4)+3);
ht4=str2num(ht4); %s4
ht5=t1(a(4)+4:a(5)+3);
ht5=str2num(ht5) ; %s5
t2=fgetl(f1);
b=strfind(t1,\'E\');;
ht6=t2(1:b(1)+3);
ht6=str2num(ht6) ; %s6
ht7=t2(a(1)+4:b(2)+3);
ht7=str2num(ht7); %s7
%Low temp co-effs
lt1=t2(b(2)+4:b(3)+3);
lt1=str2num(lt1); %s8
lt2=t2(b(3)+4:b(4)+3);
lt2=str2num(lt2) ; %s9
lt3=t2(b(4)+4:b(5)+3);
lt3=str2num(lt3) ; %s10
t3=fgetl(f1);
c=strfind(t1,\'E\');
lt4=t3(1:c(1)+3);
lt4=str2num(lt4); %s11
lt5=t3(c(1)+4:c(2)+3);
lt5=str2num(lt5); %s12
lt6=t3(c(2)+4:c(3)+3);
lt6=str2num(lt6); %s13
lt7=t3(c(3)+4:c(4)+3);
lt7=str2num(lt7); %s14
[H] = enthalpy(ht1,ht2,ht3,ht4,ht5,ht6,lt1,lt2,lt3,lt4,lt5,lt6,R,temp,global_mid_temp);
[S] = entropy (ht1,ht2,ht3,ht4,ht5,ht7,lt1,lt2,lt3,lt4,lt5,lt7,R,temp,global_mid_temp);
[C_p] = Specific_Heat(ht1,ht2,ht3,ht4,ht5,lt1,lt2,lt3,lt4,lt5,R,temp,global_mid_temp);
mkdir([\'F:\\Skill_lync\\nasaplot\\plots\\\',speciesN])
subplot(3,1,1)
plot(temp,H)
title(speciesN)
xlabel(\'Temperature\')
ylabel(\'Enthalpy\')
subplot(3,1,2)
plot(temp,S)
title(speciesN)
xlabel(\'Temperature\')
ylabel(\'Entropy\')
subplot(3,1,3)
plot(temp,C_p);
title(speciesN);
xlabel(\'Temperature\')
ylabel(\'Specific Heat\')
cd([\'F:\\Skill_lync\\nasaplot\\plots\\\',speciesN])
saveas(gcf,\'sp.png\')
cd(\'F:\\Skill_lync\')
[MW] =MolWT(speciesN);
end
Explanation
- The first 3 lines prepare the environment ready for the code to be executed
- R is the universal gas constant
- F1 is the variable set to open and read the file THERMO.dat using the command fopen
- Line 1 and line 2 are variables set to skip the commented lines by using the command fget1
- T is a variable that splits the string in line 2
- The 1st 5 lines give names
- The remaining ones gives the data of low temp at 300k medium temp at 1000k and high temp at 5000k
- Str2num converts string to numbers
- Temp is a variable which saves the values of 400 values between low temp and high temp
- The next loops are carried for all the remaining 53 elements
- a saves the values of the string after it finds the position of E
- the next set of lines are used to find the coefficients of higher and lower temperatures by converting the strings to numbers and splitting the required values from the datasheet
- [H] is a variable of the function enthalpy
- [S] is a variable of the function entropy
- [C_p] is a variable of the function specific heat
- Mkdir is used to make a directory
- Subplot is a collection of plots that are being plotted
- Plot is the command is used to plot the graph between 2 entities
- Xlabel and ylabel are used to mention the axis
- Cd is used to change the directory to the required place
- Saveas is a command used to save the plot in the desired directory
- Mw is a variable that holds the result acquired from molecular weight
Finding Enthalpy
%Enthalphy
function [H] = enthalpy(ht1,ht2,ht3,ht4,ht5,ht6,lt1,lt2,lt3,lt4,lt5,lt6,R,temp,global_mid_temp)
if temp <= global_mid_temp
H = R*temp.*(lt1 + ((lt2*temp)/2) + ((lt3*(temp).^2)/3) + ((lt4*(temp).^3)/4) + ((lt5*(temp).^4)/5) +((lt6./temp)));
else
H = R*temp.*(ht1 + ((ht2*temp)/2) + ((ht3*(temp).^2)/3) + ((ht4*(temp).^3)/4) + ((ht5*(temp).^4)/5) +((ht6./temp)));
end
Using the function enthalpy ht1,ht2,ht3,ht4,ht5,ht6,lt1,lt2,lt3,lt4,lt5,lt6,R,temp,global_mid_temp are taken as inputs and the enthalpy is calculated
Finding Entropy
%entropy
function [S] = entropy (ht1,ht2,ht3,ht4,ht5,ht7,lt1,lt2,lt3,lt4,lt5,lt7,R,temp,global_mid_temp)
if temp <= global_mid_temp
S= R.*(lt1.*log(temp) + (lt2.*temp) + (lt3*((temp).^2)/2) + (lt4*((temp).^3)/3) + (lt5*((temp).^4)/4) + lt7);
else
S= R.*(ht1.*log(temp) + (ht2.*temp) + (ht3*((temp).^2)/2) + (ht4*((temp).^3)/3) + (ht5*((temp).^4)/4) + ht7);
end
Using the function entropy ht1,ht2,ht3,ht4,ht5,ht7,lt1,lt2,lt3,lt4,lt5,lt7,R,temp,global_mid_temp
are taken as inputs and the entropy is calculated
Finding specific heat
%specific heat
function [C_p] = Specific_Heat(ht1,ht2,ht3,ht4,ht5,lt1,lt2,lt3,lt4,lt5,R,temp,global_mid_temp)
if temp <= global_mid_temp
C_p= R*(lt1 + (lt2*temp) + (lt3*(temp).^2) + (lt4*(temp).^3) + (lt5*(temp).^4));
else
C_p= R*(ht1 + (ht2*temp) + (ht3*(temp).^2) + (ht4*(temp).^3) + (ht5*(temp).^4));
end
Using the function specific heat ht1,ht2,ht3,ht4,ht5,lt1,lt2,lt3,lt4,lt5,R,temp,global_mid_temp
are taken as inputs and the specific heat is calculated
Finding molecular weight
%Molecule weight
function [MW] =MolWT(speciesN);
element={\'H\',\'C\',\'O\',\'N\',\'R\'};
rel_atm_wt=[1.00 12.01 15.99 14.00 39.06];
z=num2cell(speciesN);
MW=0;
for i=1:length(z)
for j=1:length(element)
if strcmp(z(i), element (j))
MW = MW +rel_atm_wt(j); % store value if character match.
dt = j; % store position
end
end
% used for Multiplication into the number of atom
a=str2num(z{i});
if a>1
MW = MW +rel_atm_wt(dt)*(a-1);
end
end
fprintf(\'Molecular Weight of %s : %f u\',speciesN,MW) % display molecular weight
disp(\' \')
end
- The array of elements of c h n o and ar are taken and their weights are specified
- Num2cell is a command used to convert arrays to cell
- Strcmp is a command used to compare strings and give the binary outputs based on results
- Fprint and disp are used to print and display the results of the calculated molecular weights
RESULTS
Result of all the 53 elements
The plot of O2
The plot of N2
The plot of CO2
Results on the command window
speciesN = \'O\'
Molecular Weight of O : 15.990000 u
speciesN = \'O2\'
Molecular Weight of O2 : 31.980000 u
speciesN = \'H\'
Molecular Weight of H : 1.000000 u
speciesN = \'H2\'
Molecular Weight of H2 : 2.000000 u
speciesN = \'OH\'
Molecular Weight of OH : 16.990000 u
speciesN = \'H2O\'
Molecular Weight of H2O : 17.990000 u
speciesN = \'HO2\'
Molecular Weight of HO2 : 32.980000 u
speciesN = \'H2O2\'
Molecular Weight of H2O2 : 33.980000 u
speciesN = \'C\'
Molecular Weight of C : 12.010000 u
speciesN = \'CH\'
Molecular Weight of CH : 13.010000 u
speciesN = \'CH2\'
Molecular Weight of CH2 : 14.010000 u
speciesN = \'CH2(S)\'
Molecular Weight of CH2(S) : 46.070000 u
speciesN = \'CH3\'
Molecular Weight of CH3 : 15.010000 u
speciesN = \'CH4\'
Molecular Weight of CH4 : 16.010000 u
speciesN = \'CO\'
Molecular Weight of CO : 28.000000 u
speciesN = \'CO2\'
Molecular Weight of CO2 : 43.990000 u
speciesN = \'HCO\'
Molecular Weight of HCO : 29.000000 u
speciesN = \'CH2O\'
Molecular Weight of CH2O : 30.000000 u
speciesN = \'CH2OH\'
Molecular Weight of CH2OH : 31.000000 u
speciesN = \'CH3O\'
Molecular Weight of CH3O : 31.000000 u
speciesN = \'CH3OH\'
Molecular Weight of CH3OH : 32.000000 u
speciesN = \'C2H\'
Molecular Weight of C2H : 25.020000 u
speciesN = \'C2H2\'
Molecular Weight of C2H2 : 26.020000 u
speciesN = \'C2H3\'
Molecular Weight of C2H3 : 27.020000 u
speciesN = \'C2H4\'
Molecular Weight of C2H4 : 28.020000 u
speciesN = \'C2H5\'
Molecular Weight of C2H5 : 29.020000 u
speciesN = \'C2H6\'
Molecular Weight of C2H6 : 30.020000 u
speciesN = \'CH2CO\'
Molecular Weight of CH2CO : 42.010000 u
speciesN = \'HCCO\'
Molecular Weight of HCCO : 41.010000 u
speciesN = \'HCCOH\'
Molecular Weight of HCCOH : 42.010000 u
speciesN = \'H2CN\'
Molecular Weight of H2CN : 28.010000 u
speciesN = \'HCN\'
Molecular Weight of HCN : 27.010000 u
speciesN = \'HNO\'
Molecular Weight of HNO : 30.990000 u
speciesN = \'N\'
Molecular Weight of N : 14.000000 u
speciesN = \'NNH\'
Molecular Weight of NNH : 29.000000 u
speciesN = \'N2O\'
Molecular Weight of N2O : 43.990000 u
speciesN = \'NH\'
Molecular Weight of NH : 15.000000 u
speciesN = \'NH2\'
Molecular Weight of NH2 : 16.000000 u
speciesN = \'NH3\'
Molecular Weight of NH3 : 17.000000 u
speciesN = \'NO\'
Molecular Weight of NO : 29.990000 u
speciesN = \'NO2\'
Molecular Weight of NO2 : 45.980000 u
speciesN = \'HCNO\'
Molecular Weight of HCNO : 43.000000 u
speciesN = \'HOCN\'
Molecular Weight of HOCN : 43.000000 u
speciesN = \'HNCO\'
Molecular Weight of HNCO : 43.000000 u
speciesN = \'NCO\'
Molecular Weight of NCO : 42.000000 u
speciesN = \'CN\'
Molecular Weight of CN : 26.010000 u
speciesN = \'HCNN\'
Molecular Weight of HCNN : 41.010000 u
speciesN = \'N2\'
Molecular Weight of N2 : 28.000000 u
speciesN = \'AR\'
Molecular Weight of AR : 39.060000 u
speciesN = \'C3H8\'
Molecular Weight of C3H8 : 44.030000 u
speciesN = \'C3H7\'
Molecular Weight of C3H7 : 43.030000 u
speciesN = \'CH3CHO\'
Molecular Weight of CH3CHO : 44.010000 u
speciesN = \'CH2CHO\'
Molecular Weight of CH2CHO : 43.010000 u
Reference
1.Skilllync Matlab tutorial videos
2.https://in.mathworks.com/help/index.html?s_tid=CRUX_lftnav