Project 1 - Parsing NASA thermodynamic data
AIM:
To write a code in octave to parse trhe NASA thermodynamic data file and calculate the thermodynamic properties of various gas species.
THEORY,CONCEPT AND OBJECTIVE:
To write a function which takes input the 14 coefficients and gives the output of all the enthalpy, entropy and specific heats of all the species of the "THERMO.dat" file that is given in the assignment.
The formulae are given as
`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 temperature.
Calculate the molecular weight of each of the species and print it.
Plot the graphs for the Cp, Enthalpy and Entropy for the local temperature range(low temperature range, high temperature range for each of the species.
Then, save the plots with their specific names and put them somewhere.
Data Parsing:
Data parsing is the process of extracting a collection of data from a file and converting it into useable form, by either making some important conclusions or for further study through a computer program.
CODE:
Function code to create the plots:
function [a] = plotter(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 species
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',2,'color','g')
hold on
plot(hightemp,highcp,'linewidth',2,'color','b')
hold off
ylabel('Cp');
xlabel('T');
legend('Low temperature','High temperature');
title(['Specific heat v/s Temperature Plot',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',2,'color','g')
hold on
plot(hightemp,highh,'linewidth',2,'color','b')
hold off
ylabel('H');
xlabel('T');
legend('Low temperature','High temperature');
title(['Enthalpy v/s Temperature Plot',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',2,'color','g')
hold on
plot(hightemp,highs,'linewidth',2,'color','b')
hold off
ylabel('S');
xlabel('T');
legend('Low temperature','High temperature');
title(['Entropy v/s Temperature Plot',species]);
fig= sprintf('entropy of %s.png',species); %name of the image of the plot
saveas(gca,fig); %saving the image
cd(current)
end
Main code:
%main code starts
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
atoms=['O' 'S' 'N' 'C']
m = [16 32 14 12]
%making of required plots for each of 53 species
for i=1:53
plotter(f,m,i) %calling the user-defined function plotmaker()
end
Outputs:
The folders that were created
The command window output
All the required outputs are here
THE CODE EXPLANATION:
1) The common commands to all ststements are written down, namely, clc, clear all,close all.
2)Put the THERMO.dat file in the same directory ans the octave program
3)In the main code, I will take the pointer f and make it read all the data in the THERMO.dat file, r stands for read only
4) I need to skip the first 5 lines, so, I will use the fgetl() command to move to the next lines.
5)I will then take the input of the atoms, and their molecular weight.
6)Then, I will use a for loop command as the same thing needs to be repeated 53 times again and again.
7)Then I create a function named plotter.
8) Then, using the command fgetl(), I will extract the first line out of the first 4 lines.
9) Next, I will split the extracted line using strsplit() command.
10) Then, I will store the names of the species, local low tempertaure,local high temperature,local medium temperature using the str2double() command.
11)Then, I will take the input of the 3 formulae to find out the entropy, specific heat and enthalpy and their conditions to exist, after I generate the values of the temperature,use fprintf() to display the molecular weights of the species, and using a for loop upto 3 to extract the next 3 lines from the data.
12)Then I make arrangements to save the plot using the mkdir() and the cd() commands.
13)Then, I plot the Cp v/s T, the H v/s T and the S v/s T graphs of all the species.
14) Finally, I use the end command to end the code.
ERRORS:
No major error occured during execution of the code.
CONCLUSION:
After I wrote the code, and executed it without any major errors, I got the outputs of the plotted graphs as required and the files also