Project 1 - Parsing NASA thermodynamic data using MATLAB
AIM: To write a function in MATLAB that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file (THERMO.dat).
THEORY:
The formulas are used to calculate the Specific heat (Cp), Entropy(S) and Enthalpy(H). Here, R is the universal gas constant and T is the temperature. The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients.
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:
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','b')
hold off
ylabel('Cp');
xlabel('Temperture');
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','b')
hold off
ylabel('H');
xlabel('Temperture');
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','b')
hold off
ylabel('S');
xlabel('Temperture');
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
OUTPUT:
snip of command window , showing molecular masses of different species
snip of window, showing automatically generated folders containing required plots
plots for CO2
Plots for N2
Plots for O2
EXPLANATION OF CODE AND OUTPUT:
In the main code, at first I opened the file – ‘THERMO.dat’ for reading purpose and f is acting as the pointer to the file opened. Then I skipped the unwanted lines using fgetl() command. Then I created an array containing the molecular masses of 53 species. Now, to make the required plots of different species I used a for loop with 53 iterations . Inside for loop, I called the user-defined function plotmaker().
In the user defined function plotmaker(), I passed file pointer, array of molecular masses and for loop variable as arguments. For a particular specie, I extracted the first line out of the four lines . Then splitted the extracted line with delimiter ‘ ‘ and stored the splitted parts into a cell array. Then from the cell array, I found four things – name of the specie, local low temperature, local high temperature and local medium temperature. Then I generated the values of temperature . I used two for loops (one inside the another) for extracting the coefficients. The logic used here is simple. As there are 3 lines in which the coefficients of a specie are written thus iterated the outer loop for 3 times . In outer loop, I first extracted a line and then found the positions of ‘E’ in the extracted line. The inner loop iterated for number of ‘E’ present in the extracted line . Then I found the coefficients from the extracted line according to the position of ‘E’ and stored them in a array by converting them into double data type. Then I found Cp,H and S values for different values of ‘lowtemp’ using the formulas given above in theory. Then I found Cp,H and S values for different values of ‘hightemp’ similarly. Then created the folder for saving the plots using mkdir() command. I created 3 plots – Cp vs Temperature, H vs temperature and S vs Temperature and saving each of them with particular name and png format.
In the output, I showed the screenshot of automatically saved folders containing the plots. I displayed the snip of command window showing the molecular mass of different species. Plots for o2, N2 and Co2 species are also shown.
ERROR:
Error 1. Following error was displayed stating that fprintf() function is not defined for ‘cell’ inputs in line no. 15 of user defined function plotmaker().
Error 1
STEPS TAKEN TO OVERCOME ERROR:
In the user defined function plotmaker(), I had given first bracket in line no. 5 which is incorrect because ‘spline1’ is a cell array. Thus, I changed it to second bracket which rectified my error.
RESULT: A function in MATLAB that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file (THERMO.dat) is created.
REFERENCE:
https://skill-lync.com/projects/parsing-nasa-thermodynamic-file-4