Parsing NASA Thermodynamic Data
AIM:
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.
4. Save the plots as images with appropriate names and dump them in separate folders for each species with suitable name. All these should be generated automatically.
INTRODUCTION:
In this project, we will be reading the NASA themodynamic data and then calculate the thermodynamic properties such as specific heat, enthalpy, entrophy and molecular weight of various species and plot graphs fo them.
The required data is stored in the file named 'THERMO.dat' and the data can be used to carry on the project.
The formula used for finding the Cp, H, S are given bellow,
The FIRST 7 coefficients are high-temperature coefficients and the SECOND 7 coefficients are low-temperature coefficients. The local temperature of each species ranges between 200K-3500K. Thus, the mid-temperature is 1000K. Anything that lies below that is low temperature and anything that goes beyond that is considered as high temperature.
PROCEDURE:
- Read the thermodynamic data into MATLAB using the fopen command.
- Read the three global temperatue data and skip four lines, because the file contains comments.
- Specify the species name to a variable. Open for loop and read the next line.
- In the next line , seperate the co-efficients simuntanesouly. You can do this by using strfind command to find E and then use an expression to read the data before and after that and then specify these data to different variables such as a1,a2,a3,a4......a15.
- The strings are converted into values using the str2num command. str2num command changes a string i.e '3.05' to a numerical value 3.05.
- Repeat this step for line2 and line3 and find all the values of a.
- Make a new folder where you wish to store the data using mkdir command. Assign the value species to the subfolder name that is used as parameter in mkdir command.
- Next, To calculate the specific heat, enthalpy and entrophy using these data you must use another for loop containing the the formula for calculating Cp, H and S.
- First, determine the temperature using linspace command and then create an if..else command with an argument stating if the temperature is above 1000K the first 7 co-efficients are used and if its below 1000K the second 7 co-efficients are used.
- After calculating the Cp, H, S values. They are plotted into graphs with respective to temperature using plot command.
- Specify the filepath in which you want to store the graphs and then use cd command to change the storage directory which is the same directory we created using the mkdir command.
- Save all the three plots using the saveas command.
- Now, change the directory back to where you save all the matlab programs and the folder containing you molecular weight function using cd command and specify the filepath.
- Call the molecular weight function.
- In this function the species file and a variable containing the atoms that make up the data are specfied as 'species' and 's'. 'i' and 'j' are the length of these two variables. These two variables are compared using the strcmp command inside an 'if' function. If the two values are matched the function carries out using the formula mw=mw+aw(j). Where, mw is the molecular weight and aw is the atomic weight of the respective species.
- Using str2double command the string in species are converted into integers or values. These values are stored ina variable 'n'. If the n is greater than 1 fo special atoms such AR. The following fomula is executed. mw=mw+aw(j)*(n-1)
- After the molecular weight is calculated for a species they are written in a new text file by using fopen command followed by fprintf command to print the data. In fopen function we use append mode to write the molecular data of all the species in one txt file.
- The entire function is witten inside a for loop function with a counter variable with value 1:53, because there are 53 lines that needs to be parsed excluding the last line which is 'END'.
- Close the file using fclose command.
CODE 1: Main Function
clear all
close all
clc
%Universal Gas Constant
R= 8.314;
f1= fopen('THERMO.dat','r');
Title= fgetl (f1);
G= fscanf (f1,'%f',[1 3]);
%Temperature
global_low_temp= G(1);
global_mid_temp= G(2);
global_high_temp= G(3);
%Skiping the comments
for i=1:4;
fgetl(f1);
end
for i=1:53;
tline=fgetl(f1);
% Reading a line and separating the co-efficients
species= tline(1:8)
% Reading co-efficients simultaneously
line1= fgetl(f1);
%Finding the the string E and seperating co-efficients using it
A1=strfind(line1,'E');
%Finding Individual Co-efficients
a1=line1(1:A1(1)+3);
a1=str2num(a1);
a2=line1(A1(1)+4:A1(2)+3);
a2=str2num(a2);
a3=line1(A1(2)+4:A1(3)+3);
a3=str2num(a3);
a4=line1(A1(3)+4:A1(4)+3);
a4=str2num(a4);
a5=line1(A1(4)+4:A1(5)+3);
a5=str2num(a5);
line2=fgetl(f1);
a6=line1(1:A1(1)+3);
a6=str2num(a6);
a7=line1(A1(1)+4:A1(2)+3);
a7=str2num(a7);
a8=line1(A1(2)+4:A1(3)+3);
a8=str2num(a8);
a9=line1(A1(3)+4:A1(4)+3);
a9=str2num(a9);
a10=line1(A1(4)+4:A1(5)+3);
a10=str2num(a10);
line3=fgetl(f1);
a11=line1(1:A1(1)+3);
a11=str2num(a11);
a12=line1(A1(1)+4:A1(2)+3);
a12=str2num(a12);
a13=line1(A1(2)+4:A1(3)+3);
a13=str2num(a13);
a14=line1(A1(3)+4:A1(4)+3);
a14=str2num(a14);
a15=line1(A1(4)+4:A1(5)+3);
a15=str2num(a15);
%Storing the data in a subfolder
subfolder_name= species;
mkdir(['C:\Users\joelk\Desktop\MATLAB Programs\Project- Parsing Thermodynamic Data\ Graph Data-',subfolder_name]);
%Calculating Cp, Enthalpy and Entrophy
%Considering the local temperature range
%Low Temperature range < 1000K (200K-1000K)
%Average Temperature= 1000k
%High Temperature range= > 1000K (1000K-35000K)
T=linspace(200,3500,100);
for j=1:length(T);
if T(j)>1000
Cp=R*(a1+(a2.*T)+a3.*(T.^2)+a4.*(T.^3)+a5.*(T.^4));
H=R*T.*((a1+(a2.*T)/2)+(a3.*T.^2)/3+(a4.*T.^3)/4+(a5.*T.^4)/5+a6.*T);
S=R*(a1*log(T)+a2.*T+(a3.*T.^2)/2+(a4.*T.^3)/3+ (a5.*(T.^4)/4)+a7);
else
Cp=R*(a8+(a9.*T)+a10.*(T.^2)+a11.*(T.^3)+a12.*(T.^4));
H=R*T.*((a8+(a9.*T)/2)+(a10.*T.^2)/3+(a11.*T.^3)/4+(a12.*T.^4)/5+a13.*T);
S=R*(a8*log(T)+a9.*T+(a10.*T.^2)/2+(a11.*T.^3)/3+ (a12.*(T.^4)/4)+a14);
end
end
%Plotting
figure(1)
plot(T,Cp,'linewidth',2,'color','r')
title(species)
xlabel('Temperature')
ylabel('Specific Heat')
grid on
figure(2)
plot(T,H,'linewidth',2,'color','b')
title(species)
xlabel('Temperaute')
ylabel('Enthalpy')
grid on
figure(3)
plot(T,S,'linewidth',2,'color','g')
title(species)
xlabel('Temperature')
ylabel('Entrophy')
grid on
%Directory for saving the graphs
filepath=(['C:\Users\joelk\Desktop\MATLAB Programs\Project- Parsing Thermodynamic Data\ Graph Data-',subfolder_name]);
cd(filepath)
saveas(1,'Graph- T vs Cp.jpg')
saveas(2,'Graph- T vs H.jpg')
saveas(3,'Graph- T vs S.jpg')
%Calling Molecular Weight function
cd('C:\Users\joelk\Desktop\MATLAB Programs')
Molecular_weight= molecular_weights(species)
%Saving the molecular weight
f2=fopen('Molecular Weights.txt','a+');
fprintf(f2,'Molecular Weight of %s is %f \n',species,Molecular_weight);
end
fclose(f1);
CODE 2: Molecular Weight Function
function [mw]= molecular_weights(species)
s= {'H' 'C' 'N' 'O' 'AR'};
%Atomic weights of each species
aw= [1 12 14 16 40];
%Intial molecular weight
mw= 0;
for i= 1:length(species)
for j= 1:length(s)
if strcmp(species(i),s(j))
mw= mw + aw(j);
%Sorting the position of matched element
position=j;
end
end
n= str2double(species(i));
if n>1
mw=mw+aw(position)*(n-1);
end
end
OUTPUT:
Folders containing the graphs for various species
Plot for O2 :
Figure 1:
Figure 2:
Figure 3:
Plot for N2:
Figure 1:
Figure 2:
Figure 3:
Plot for CO2:
Figure 1:
Figure 2:
Figure 3:
Molecular Weight calculated simultanesouly in command window
LINKS:
https://drive.google.com/drive/folders/1X-DczZidhbZBxV4OkW-7M7bxQHubEkAJ?usp=sharing
The above link contains the project folder containing all the graph data on Cp, H and S fo various species.
https://drive.google.com/open?id=1SHN08wG2i6F8uHWo4InS7b4E2it2AgE_
The above link contains the text file containing the molecular weights for all the species.
REFERENCES:
https://in.mathworks.com/help/matlab/ref/mkdir.html?searchHighlight=mkdir&s_tid=doc_srchtitle
https://in.mathworks.com/help/matlab/ref/strcmp.html?s_tid=srchtitle