Project 1 - Parsing NASA thermodynamic data
AIM:
To write a program in MATLAB/OCTAVE to parse the thermodynamic data file and then calculate the thermodynamic properties of various gaseous species.
EQUATION/FORMULA USED:
write a function that extracts the 14 coefficients and calculates the enthalpy, entropy, and specific heats for all the species in the 'THERMO.dat' file. The formulae are shown below.
`(Cp)/R = a1 + a2.T + a3.T^2 + a4.T^3 + a5.T^4`
`(H)/(RT) = a1 + a2.T/2 + a3.T^2/3 + a4.T^3/4 + a5.T^4/5 + a6. 1/T`
`(S)/(R) = a1 lnT + a2.T + a3.T^2/2 + a4.T^3/3 + a5.T^4/4 + a7`
Here R is the universal gas constant, T is the temeprature.
OBJECTIVE:
- To write a function that extracts the 14 coefficients
- To calculate the enthalpy, entropy and specific heats for all the species in the data file.
- To calculate the molecular weight of each species and display it in the command window.
- To plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature: high temperature) specific for each species.
THEORY:
Data parsing is a method where one string of data gets converted into a different type of data. When we receive your data in raw HTML, a parser will take the said HTML and transform it into a more readable data format that can be easily read and understood.
Parsing a file means reading in a data stream of some sort and building an in-memory model of the semantic content of that data. This aims at facilitating performing some kind of transformation on the data.
BODY:
- CODE:
MAIN CODE:
clear all
close all
clc
f = fopen('THERMO.dat','r'); %opening the file for reading purpose;here, f is the pointer to the file.
%skipping the unwanted lines i.e. the first five lines are not required
fgetl(f);
fgetl(f);
fgetl(f);
fgetl(f);
fgetl(f);
%molecular weight for different species
m = [16 32 1 2 17 18 33 34 12 13 14 14 15 16 28 44 29 30 31 31 32 25 26 27 28 29 30 42 41 42 28 27 31 14 29 44 15 16 17 30 44 43 43 43 42 26 41 28 40 44 43 44 43 ] ;
%making of required plots for each of 53 species
for i=1:53
plotmaker(f,m,i) %calling the user-defined function plotmaker()
end
Function Code:
function [a] = plotmaker(f,m,i)
line1 = fgetl(f); %extracting the first line out of four for a particular species
spline1 = strsplit(line1,' '); %splitting the above extracted line using strplit(); here, ' ' is the delimitter
species=spline1{1}; %storing the name of the specie ; here {} is necessary because spline is a cell array
localtemplow=str2double(spline1{6}); %storing the local low temperature for a particular specie
localtemphigh=str2double(spline1{7}); %storing the local high temperature for a particular specie
localtempmid=str2double(spline1{8}); %storing the local medium temperature for a particular specie
% generating the values of temperature
lowtemp = linspace(localtemplow,localtempmid,100);
hightemp=linspace(localtempmid,localtemphigh,100);
%displaying the molecular weight of the specie
fprintf('molecular weight of %s is = %f u \n',species,m(i))
c=1;
for i=1:3
tline=fgetl(f); %extracting the next line out of four for a particular specie
E=strfind(tline,'E'); %finding the position of E in the above extracted line
for j=1:length(E)
coeff(c)=str2double(tline(E(j)-11:E(j)+3)); %extracting the coefficients of data type double
c=c+1;
end
end
R=8.314; % universal gas constant
%finding Cp,H and S values for different values of lowtemp
for i=1:100
lowcp(i)=R*(coeff(8)+coeff(9)*lowtemp(i)+coeff(10)*lowtemp(i)^2+coeff(11)*lowtemp(i)^3+coeff(12)*lowtemp(i)^4);
lowh(i)=R*lowtemp(i)*(coeff(8) + coeff(9)*lowtemp(i)/2 + coeff(10)*(lowtemp(i)^2)/3 + coeff(11)*(lowtemp(i)^3)/4+coeff(12)*(lowtemp(i)^4)/5+coeff(13)/lowtemp(i));
lows(i)=R*(coeff(8)*log(lowtemp(i))+coeff(9)*lowtemp(i)+coeff(10)*(lowtemp(i)^2)/2+coeff(11)*(lowtemp(i)^3)/3+coeff(12)*(lowtemp(i)^4)/4+coeff(14));
end
%finding Cp,H and S values for different values of hightemp
for i=1:100
highcp(i)=R*(coeff(1)+coeff(2)*hightemp(i)+coeff(3)*hightemp(i)^2+coeff(4)*hightemp(i)^3+coeff(5)*hightemp(i)^4);
highh(i)=R*hightemp(i)*(coeff(1) + coeff(2)*hightemp(i)/2 + coeff(3)*(hightemp(i)^2)/3 + coeff(4)*(hightemp(i)^3)/4+coeff(5)*(hightemp(i)^4)/5+coeff(6)/hightemp(i));
highs(i)=R*(coeff(1)*log(hightemp(i))+coeff(2)*hightemp(i)+coeff(3)*(hightemp(i)^2)/2+coeff(4)*(hightemp(i)^3)/3+coeff(5)*(hightemp(i)^4)/4+coeff(7));
end
%making folder for saving the plots
nasagraphs = species;
mkdir(nasagraphs);
current=pwd;
cd(nasagraphs);
%plotting of Cp vs temperature
figure (1)
plot(lowtemp,lowcp,'linewidth',3,'color','b')
hold on
plot(hightemp,highcp,'linewidth',3,'color','r')
hold off
ylabel('Cp');
xlabel('Temperture');
legend('Low temperature','High temperature');
title(['Specific heat vs temperature ',species]);
fig= sprintf('specific heat of %s.png',species); %name of the image of the plot
saveas(gca,fig); %saving the image
%plotting of H vs Temperature
figure (2)
plot(lowtemp,lowh,'linewidth',3,'color','b')
hold on
plot(hightemp,highh,'linewidth',3,'color','r')
hold off
ylabel('H');
xlabel('Temperture');
legend('Low temperature','High temperature');
title(['Enthalpy vs temperature ',species]);
fig= sprintf('enthalpy of %s.png',species); %name of the image of the plot
saveas(gca,fig); %saving the image
%plotting of S vs Temperature
figure (3)
plot(lowtemp,lows,'linewidth',3,'color','b')
hold on
plot(hightemp,highs,'linewidth',3,'color','r')
hold off
ylabel('S');
xlabel('Temperture');
legend('Low temperature','High temperature');
title(['Entropy vs temperature ',species]);
fig= sprintf('entropy of %s.png',species); %name of the image of the plot
saveas(gca,fig); %saving the image
cd(current)
end
- STEP-WISE EXPLANATION OF THE CODE:
- This program is written and executed using octave.
- Starting with all the general command e. clear all, close all & clc.
- Inputting all the code comment at all the required location starting with %.
- Put the THERMO.dat file in the same directory where the octave program is saved.
- Then we will write the main code.
- We will take the pointer f and make it read all the data in the THERMO.dat file, r stands for read-only.
- Skipping the first 5 lines, using fgetl() command will result to move to the next lines.
- Then the code for the molecular weight is written.
- Then the further code is written in a loop so that the things get repeated 53 times again and again.
- Then function code is written named plotmaker.
- Starting with a function code followed by fgetl(), then strsplit command.
- Then the local temperature is stored using the str2double() command.
- Then we will write code to find out the entropy, specific heat and enthalpy and their condition to exist.
- Then writing the code to display the molecular weight of the species.
- Then we save the plot using the mkdir() and the cd() commands.
- Then the code is executed and the plot of Cp v/s T, H v/s T and S v/s T graphs of all the species are generated.
- Finally, we use the end command to end the code.
- ERROR FACED WHILE PROGRAMMING:
No major error during the execution of code.
OUTPUTS:
folder saved:
PLOT FOR O2:
PLOT FOR N2:
PLOT FOR CO2:
MOLECULAR WEIGHT:
RESULT:
Hence, we have successfully done all the calculation and also successfully plotted the graph.