Write the code to Parsing NASA thermodynamic data
Objective- Write code to parse nasa thermodynamc data file and calculate the molecular weight and thermodynamic properties of species.
Parsing-
Parsing, syntax analysis, or syntactic analysis is the process of analysing a string of symbols, either in natural language, computer language or data structures.
Challenge-
1. Write a function that extracts the 14 co-efficients 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.
Code-
clear all
close all
clc
f1=fopen(\'THERMO.dat\',\'r\')
fgetl(f1)
tline2 = fgetl(f1)
B= strsplit(tline2)
low_global_temp= str2num(B{2});
mid_global_temp= str2num(B{3});
high_global_temp= str2num(B{4});
%skipping unwanted lines
for i=1:3
fgetl(f1)
end
for j=1:53
tline2=fgetl(f1)
D=strsplit(tline2)
AC=(D{1});
LOW_TEMP_COEFF=str2num(D{end-3})
MID_TEMP_COEFF=str2num(D{end-1})
HIGH_TEMP_COEFF=str2num(D{end-2})
LTC= linspace( LOW_TEMP_COEFF, MID_TEMP_COEFF,100);
HTC=linspace( MID_TEMP_COEFF, HIGH_TEMP_COEFF,100);
%Reading the all variables
tline1= fgetl(f1);
A=findstr(tline1,\'E\')
A1=str2num(tline1(1:A(1)+3));
A2=str2num(tline1(A(1)+4:A(2)+3));
A3=str2num(tline1(A(2)+4:A(3)+3));
A4=str2num(tline1(A(3)+4:A(4)+3));
A5=str2num(tline1(A(4)+4:A(5)+3));
tline2=fgetl(f1)
B=findstr(tline2,\'E\');
A6=str2num(tline2(1:B(1)+3));
A7=str2num(tline2(B(1)+4:B(2)+3));
A8=str2num(tline2(B(2)+4:B(3)+3));
A9=str2num(tline2(B(3)+4:B(4)+3));
A10=str2num(tline2(B(4)+4:B(5)+3));
tline3=fgetl(f1)
C=findstr(tline3,\'E\')
A11=str2num(tline3(1:B(1)+3));
A12=str2num(tline3(C(1)+4:B(2)+3));
A13=str2num(tline3(C(2)+4:B(3)+3));
A14=str2num(tline3(C(3)+4:B(4)+3))
%universal gas constant
R=8.3145
%calculating values of CP, enthalpy and entropy for low temperature
for k=1:length(LTC);
CP1(k) = ((A1 + A2*(LTC(k)) + A3*(LTC(k).^2) + A4*(LTC(k).^3) + A5*(LTC(k).^4))*R);
H1(k) = (A1 +A2*(LTC(k)/2) + A3*((LTC(k).^2)/3) + A4*((LTC(k).^3)/4) + A5*((LTC(k).^4)/5) + (A6/LTC(k)))*(R*LTC(k));
S1(k) = (A1*log(LTC(k))+ A2*LTC(k)+ A3*((LTC(k).^2)/2)+ A4*((LTC(k).^3)/3)+ A5*((LTC(k).^4)/4)+A7)*R;
end
%calculating values of CP, enthalpy and entropy for high temperature
for k= 1:length(HTC);
CP2(k) = ((A8 + A9*(HTC(k)) + A10*(HTC(k).^2) + A11*(HTC(k).^3) + A12*(HTC(k).^4))*R);
H2(k) = (A8 +A9*(HTC(k)/2) + A10*((HTC(k).^2)/3) + A11*((HTC(k).^3)/4) + A12*((HTC(k).^4)/5) + (A13/HTC(k)))*(R*HTC(k));
S2(k) = (A8*log(HTC(k))+ A9*HTC(k)+ A10*((HTC(k).^2)/2)+ A11*((HTC(k).^3)/3)+ A12*((HTC(k).^4)/4)+A14)*R;
end
%calling molecular weight function
molecular_weight(j) = mw(AC)
%saving folders
mkdir([\'C:\\Users\\dell\\Desktop\\matlab\\fileParsing\\\',AC])
cd([\'C:\\Users\\dell\\Desktop\\matlab\\fileParsing\\\',AC])
%ploting graphs
figure(1)
plot(LTC,CP1,\'linewidth\',3);
hold on
plot(HTC,CP2,\'linewidth\',3);
hold off
title(\'TEMPERATURE vs SPECIFIC_HEAT\')
xlabel(\'TEMPERATURE\')
ylabel(\'SPECIFIC_HEAT\')
legend(\'low temperature\',\'high temperature\')
saveas(gcf,\'file1.png\');
figure(2)
plot(LTC,H1,\'linewidth\',3);
hold on
plot(HTC,H2,\'linewidth\',3);
hold off
title(\'TEMPERATURE vs ENTHALPY\')
xlabel(\'TEMPERATURE\')
ylabel(\'ENTHALPY\')
legend(\'low temperature\',\'high temperature\')
saveas(gcf,\'file2.png\');
figure(3)
plot(LTC,S1,\'linewidth\',3);
hold on
plot(HTC,S2,\'linewidth\',3);
hold off
title(\'TEMPERATURE vs ENTROPY\')
xlabel(\'TEMPERATURE\')
ylabel(\'ENTROPY\')
legend(\'low temperature\',\'high temperature\')
saveas(gcf,\'file3.png\');
cd(\'C:\\Users\\dell\\Desktop\\matlab\');
end
STEP1-
First we open the data file and read, fgetl(f1) command used for skipping the unwanted lines.
strsplit command is for split line after every space.
then we reading value of low global temp, mid global temp and high global temp.
str2num converts character arrays to numeric arrays.
Now use for loop for skipping 3 lines this are comments(unwanted lines).
STEP2-
Now we create for loop for 53 species like \'o\',\'o2\',\'N2\',etc. and read the temperature of each species.
after that we create range of low temperature and high temperature with interval of 100.(LTC and HTC)
we have 14 coefficient in that first 7 are low coefficient and other 7 are high coefficient.
STEP3-
In this coefficient we take E as reference and we define all the coefficient (A1 ,A2 ,A3 ,...A14)
R is universal gas constant. Now we putting the above equations for calculating specific heat , entropy and enthalpy for low and high temperature. (CP1, H1, S1 and CP2, H2,S2).
STEP4-
Now calling the function to calculate molecular weight. The function is given below, in this two array are created i.e. elements and atomic weight
Two loops create for calculate atomic mass.
Next three steps for multiplication in number of atoms.
function Mw = mw(species_name);
elements =[\'H\',\'C\',\'O\',\'N\',\'A\',\'S\'];
atomic_weight =[1.00 12.01 15.99 14.00 39.95 32.06];
Mw =0
for i=1:length(species_name);
for j=1:length(elements);
if strcmp(species_name(i),elements(j))
Mw = Mw+ atomic_weight(j)
a=j;
end
end
n = str2num(species_name(i));
if n>1
Mw =Mw+ atomic_weight(a)*(n-1);
end
end
end
STEP5-
Now we create directory and folders for each species to save plots. (mkdir)
After that ploting graphs of CP, H, and S.
Molecular weight-
Plots of \'OXYGEN\'-
Plots of \'NITROGEN\'-
Plots of \'CARBON DIOXIDE\'-
Directory-
Conclusion-
Plotted graph of CP, enthalpy and entropy for differen species.
successfully saved all plots in directory with making folders.