Parsing NASA thermodynamic data in Matlab
AIM- Parsing the NASA thermodynamic data in Matlab
OBJECTIVE -
- To write a function that extracts the 14 coefficients and calculates the enthalpy, entropy and specific heats for all the species
- 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.
- To 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.
GOVERNING EQUATIONS –
THEORY –
The parsing is done to extract the data by breaking it down and making it compatible for easy translation into another language.
SPECIFIC HEAT – specific heat can be defined as the amount of heat required per unit mass to raise the temperature by 1deg C. Hence it is important as it will give an indication of how much energy will be required to heat or cool an object of a given mass by a given amount.The SI unit is KJ/(Kg.K).
They are of two types: -
- Specific heat at constant pressure (Cp)
- Specific heat at constant volume (Cv)
The transferred heat depends on three factors:
- The change in temperature
- the mass of the system
- the substance and phase of the substance.
ENTHALPY – enthalpy can be defined as the total heat content of the system and is sum of internal energy and work done on or by the system. The change is enthalpy is defined as the product of specific heat at constant pressure and temperature range in which particular thermodynamic process happened. It is a property of the thermodynamic system. The SI unit is Joule
The equation for enthalpy and enthalpy change is given by: -
h= u + Pv
dh = Cp*Tdt
ENTROPY – entropy is the extensive property to define the randomness of the process. According to second law of thermodynamics, entropy either remains constant or increases. The increase in thermodynamics property is seen in irreversible change and in reversible change, it remains constant. The SI unit is J/K
The equation for change in entropy is given by: -
dS = dQ/ T
CODE EXPLANATION
The code is to parse the given data from the thermodynamic results to Matlab access data for 14 coefficients
Further calculations are done for specific heat, enthalpy and entropy from the provided equations.
Later, molecular weight for all the 53 species are calculated by running the code.
The codes are given below with explanation
Two separate functions are generated with ‘for’ loops in order to do the calculation for 2 ranges of temperatures. The lower range takes the coefficients [ a_1: a_7] and calculate respective Cp, h and S values. Similar trend is seen in high range and coefficients [a1: a7]. The ‘for’ loop is provided so that each temperature has its own values for Cp, h and S. These functions will be called later in the main code.
function [cp, H, S] = thermhigh(a1,a2,a3,a4,a5,a6,a7,T)
R = 8.314;
z = zeros(length(T));
ct= 0;
for i =1: length(T)
cp(i) = R*(a1+ (a2.*T(i)) + (a3.*T(i)^2) + (a4.*T(i)^3) + (a5.*T(i)^4));
H(i) = R.*T(i)*(a1+ (a2.*T(i)/2) + (a3.*T(i)^2/3) + (a4.*T(i)^3/4) + (a5.*T(i)^4/5)+ ( a6 ./T(i)));
S(i) = R*((a1.*log(T(i)))+ (a2.*T(i)) + (a3.*T(i)^2/2) + (a4.*T(i)^3/3) + (a5.*T(i)^4/4) + a7);
ct= ct+1;
end
end
function [Cp, h, s] = therm_low(a_1,a_2,a_3,a_4,a_5,a_6,a_7,t)
R = 8.314;
z = zeros(length(t));
xt=0;
for i =1: length(t)
Cp(i) = R*(a_1+ (a_2.*t(i)) + (a_3.*t(i)^2) + (a_4.*t(i)^3) + (a_5.*t(i)^4));
h(i) = R.*t(i)*(a_1+ (a_2.*t(i)/2) + (a_3.*t(i)^2/3) + (a_4.*t(i)^3/4) + (a_5.*t(i)^4/5)+ (a_6./t(i)));
s(i) = R*((a_1.*log(t(i)))+ (a_2.*t(i)) + (a_3.*t(i)^2/2) + (a_4.*t(i)^3/3) + (a_5.*t(i)^4/4) + a_7);
xt=xt+1;
end
end
The main code consists of following commands –
fopen – to open a file in matlab
fgetl – to get the next line as a string
strsplit – to split main string into smaller strings using a delimiter
strnum – to convert string to number
currentFolder = pwd – to returns path to current folder
mkdir - to create a new folder
cd – change directory
saveas – to save image in desired format
strcmp – to compare two different strings
strdouble – to convert string to double format
fprintf – to print the line as such
close all
clear
clc
f1 = fopen('THERMO.dat','r'); %to open the thermo data file
fgetl(f1); %to get the first or next line as a string
a=fgetl(f1);
b= strsplit(a,' '); %to split a string
g_low_temp = str2num(b{2}); %to change string to number to get lower temperature
g_mid_temp = str2num(b{3}); %for mid temp
g_high_temp= str2num(b{4}); %for high temp
for i=1:3 %to skips next three lines
fgetl(f1);
end
for l= 1:53 %to run for all the species
a_b = fgetl(f1);
b = strsplit(a_b,' ');
species_name = b{1}; %to get the name of species
n = length(b);
l_low_temp = str2num(b{n-3});
l_mid_temp = str2num(b{n-1});
l_high_temp= str2num(b{n-2});
a_c = fgetl(f1);
ax = strfind(a_c,'E');
%Calculation for coefficients
%line 1
a1 = str2num(a_c(1:ax(1)+3));
a2 = str2num(a_c(ax(1)+4: ax(2)+3));
a3 = str2num(a_c(ax(2)+4: ax(3)+3));
a4 = str2num(a_c(ax(3)+4: ax(4)+3));
a5 = str2num(a_c(ax(4)+4: ax(5)+3));
%line2
a_c= fgetl(f1);
ax_1 = strfind(a_c,'E');
a6 = str2double(a_c(1:ax_1(1)+3));
a7 = str2num(a_c(ax_1(1)+4: ax_1(2)+3));
a_1 = str2num(a_c(ax_1(2)+4: ax_1(3)+3));
a_2 = str2num(a_c(ax_1(3)+4: ax_1(4)+3));
a_3 = str2num(a_c(ax_1(4)+4: ax_1(5)+3));
%line3
a_c= fgetl(f1);
ax_2 = strfind(a_c,'E');
a_4 = str2num(a_c(1:ax_2(1)+3));
a_5 = str2num(a_c(ax_2(1)+4: ax_2(2)+3));
a_6 = str2num(a_c(ax_2(2)+4: ax_2(3)+3));
a_7 = str2num(a_c(ax_2(3)+4: ax_2(4)+3));
%calling of functions
t = linspace(l_low_temp,l_mid_temp,100);
[Cp, h, s] = therm_low(a_1,a_2,a_3,a_4,a_5,a_6,a_7,t);
T = linspace(l_mid_temp,l_high_temp,100);
[cp, H, S] = thermhigh(a1,a2,a3,a4,a5,a6,a7,T);
%making of folder and saving files in it
Folder = sprintf('%d %s',l,species_name);
currentFolder = pwd;
mkdir(Folder);
cd(Folder);
figure(1)
plot([t T],[Cp cp],'linewidth',3,'color','r'); %plotting the graph
xlabel('TEMPERATURE');
ylabel('SPECIFIC HEAT');
title('Specific Heat vs Temperature ');
image1 = sprintf('cp vs t of %s',species_name); %naming the file
saveas(gcf,image1,'jpeg'); %saving file in jpeg format
figure(2)
plot([t T],[h H],'linewidth',3);
xlabel('TEMPERATURE');
ylabel('ENTHALPY');
title('Enthalpy vs Temperature ');
image2 = sprintf('h vs t of %s',species_name);
saveas(gcf,image2,'jpeg');
figure(3)
plot([t T],[s S],'linewidth',3,'color','g');
xlabel('TEMPERATURE')
ylabel('ENTROPY')
title('Entropy vs Temperature ')
image3 = sprintf('s vs t of %s',species_name);
saveas(gcf,image3,'jpeg');
Folder = cd(currentFolder);
%for molecular mass calculation
element = ['C','O','N','H','A'];
mass = [12.011,15.999,14.007,1.008,39.948];
z = zeros(length(species_name));
mol_mass = 0;
for i = 1: length(species_name)
for j = 1: length(element)
if strcmp(species_name(i), element(j))==1
mol_mass = mol_mass + mass(j);
position = j;
end
end
x = str2double(species_name(i));
if x>1
mol_mass= mol_mass + mass(position)*(x-1);
end
end
fprintf('Molecular weight of %s:%f amu n', species_name,mol_mass)
end
The main code consists of a for loop which is from 1: 53 as there are 53 species in total. Now within the loop, code fgetl is used to gain the string for specific row in the data. The pattern is repeated after every 4 rows. Using different commands like strsplit (for each row) and strnum (for each splitted strings), the coefficients are found and also the calling of function is done. The range given for strnum is for gain particular coefficent which is within that range. Thedelimiter was taken as 'E'. Calling of functions provide the specific heat, enthalpy and the entropy for each temperature for every species until the loop ends.
The folder is defined with the name of species using a code. Later the pwd command is used to return path to created folder. This will work as the loop proceeds. Now a folder is created using mkdir command and cd command changes the directory to new folder. Further, three different graphs are plotted for each species. The graphs are of specific heat vs temperature, enthalpy vs temperature and entropy vs temperature. The plots are labelled and named succeeded by saving then in jpeg format. The pwd command is again used, to return the code to a new folder.
Now the process is to calculate the molecular mass of the species. The atomic mass of each element is formed to be two separate strings. This is now fed to the nested ‘for’ loop in which the limits are for length of species and the length of element string. Strcmp command is used to compare the elements and the species and for that a feed (atomic mass here) is given to the void named as mass. In this loop each value is compared and it’s added till the value is attained. Further the other loop is for length of string greater than 1. The overall molar mass is calculated.
RESULTS -
O2
N2
CO2
The screenshot of folder with species details: -
Molecular Weight of different species -
ERRORS FACED
The error faced in the coding was related with the molecular weight Argon element, in which AR notation started to give 2 different elements leading to 'index exceeds the number of array elements(5)' error showed up.
Replacing AR with A solved the problem as the number of array elemrnts were read as 5 by the software and further calculations continued.
CONCLUSION-
- The parsing of data has been done and all the 14 coefficients were taken from thermodynamic data from NASA. Further Cp, h and S were calculated and plotted using different commands.
- The required plots and screeenshots were provided. The calculation gave an insight to the behaviour of enthalpy, specific heat and entropy under temperature change.
- It was observed that in most of the cases, the specific heat and entropy gave the concave up curve while enthalpy gave concave down curve.
- In case of Argon, the specific heat curve was a constant curve and enthalpy curve was linear. This signifies how nobles gases act differently from the other hydrocarbons and other compounds.
- Also, a pattern with enthalpy was observed in monoatomic species with linear graph for enthalpy vs temperature.