Project 1 - Parsing NASA thermodynamic data
File Parsing is the technique of reading information from a file so that it can be used in a computer program to perform useful tasks. This report explains in detail such a program, which reads data from the NASA thermodynamic data file and calculates the molecular weight of different species. Further, the program also plots the variation of different properties with temperature.
The given file ‘THERMO.DAT’ contains the thermodynamic data of 53 different species, expressed in the form of 14 coefficients. For each of the species, the coefficients are defined over a specific local temperature range. The temperature range given has three values; lower value, mid value and high value. The first 7 coefficients are valid when the temperature ranges between mid and high value and the second 7 is valid between low and mid temperature value.
Some of the tasks performed by the program are as given below:
- Reads the different species name, calculates the molecular weight and displays it in the command window.
- Reads the local temperature range assigned for each species.
- Reads the 14 coefficients for each of the species and calculates the specific heat, enthalpy and entropy.
- Plots the variation of Cp, H and s over the given local temperature range
- Saves the figures in separate folders, named based on the species name.
Explanation of the program:
- The given file THERMO.DAT is opened using the fopen command in the reading mode. Since the program aims to plot the property variation with respect to the local temperatures, the global temperature can be neglected. The header information of the data file can also be neglected. Hence the first 5 lines of the file can be skipped by using the fgetl command.
- It can also be noticed that the pattern in which the data is given repeats for each of the species given. Hence all the following commands are put in a loop, which repeats 53 times (since 53 different species is present).
- The first line of the species is read using the fgetl command and saved in the tline variable. It can be noticed in the data file that, before the local temperature values are mentioned, the character ‘G’ followed by a space is common for all the species. Hence its position is found and stored in variable ‘a’. Further the length of the tline variable is stored in ‘b’.
- The local temperature values lies in the range of ‘a+4’ to ‘b-5’ and this string is stored in A. The string A is split at each of the space character and each of these values is converted into the numerical value using str2num command. The values are stored as low_temp, mid_temp and high_temp.
- The species names are the first few characters in the tline and this can be read, if we find the position of the space character that follows the name. This character is stored in ‘g’ variable and the species name is between the character 1 and ‘g-1’.
- For finding the molecular weight of the species, we store the different elements and its atomic weights in two different arrays, but in corresponding positions. For each position of the species name, the element is compared and if found same its atomic weight is added to the MW variable. Further, if any of the characters in the species name is a number, the product of atomic weight of the previous element and the (number-1) is added to MW. This value is printed for each species.
- After completing the above tasks, the next line in the file is read using the fgetl command and the position of ‘E’ is found in the line. This is to separate each of the coefficients from the line. The corresponding positions of each coefficient is found and it is stored in different variables. The same process is repeated for the next three lines such that all 14 coefficients are found and stored.
- The low temperature range is in between the low and mid temperature values and the high temperature zone between the mid and high value. Two separate arrays are created to store 50 values in each range and the values for Cp, H and s are found using the below relations.
9. Using these values three different figures are plotted with each figure describing the variation of a property in the local temperature range.
10. For saving the figures a new folder is created using the mkdir command and the directory is updated to this folder, using the cd command. Finally, each figure is saved with respective titles using the saveas command.
11. All the steps from 3-11 are repeated to perform the tasks for all the different species.
Program:
clear all
close all
clc
%Open THERMO.DAT file
f = fopen('THERMO.DAT','r');
%Reading and skipping first 5 lines
fgetl(f);
fgetl(f);
fgetl(f);
fgetl(f);
fgetl(f);
%The following set of commands has to be repeated 53 times (since there are 53 species)
for k = 1:53
%Reading the next line to find 'G ' and string length
tline = fgetl(f);
a = findstr(tline,'G ');
b = strlength(tline);
%Find the species name
g = findstr(tline,' ');
species_name = tline(1:g-1);
fprintf('\nSpecies Name - %s\n',species_name);
%Finding molecular weight of the species
Element = ['O' 'H' 'C' 'N' 'S' 'A'];
a_w = [15.999 1.00784 12.0107 14.0067 32.065 39.948];
MW = 0;
for i = 1:strlength(species_name)
for j = 1:length(Element)
p = strcmp(species_name(i),Element(j));
if p == 1
MW = MW + a_w(j);
m = j;
end
end
n = str2num(species_name(i));
if n>1
MW = MW + a_w(m)*(n-1);
end
end
fprintf('Molecular weight of %s is %f\n',species_name,MW);
%Finding the local temperature ranges
A = tline(a+4:b-5);
B = strsplit(A,' ');
low_temp = str2num(B{1});
high_temp = str2num(B{2});
mid_temp = str2num(B{3});
%Reading each line and finding the coefficients
tline1 = fgetl(f);
c = findstr(tline1,'E');
a1 = str2num(tline1(1:c(1)+3));
a2 = str2num(tline1(c(1)+4:c(2)+3));
a3 = str2num(tline1(c(2)+4:c(3)+3));
a4 = str2num(tline1(c(3)+4:c(4)+3));
a5 = str2num(tline1(c(4)+4:c(5)+3));
tline2 = fgetl(f);
d = findstr(tline2,'E');
a6 = str2num(tline2(1:d(1)+3));
a7 = str2num(tline2(d(1)+4:d(2)+3));
a8 = str2num(tline2(d(2)+4:d(3)+3));
a9 = str2num(tline2(d(3)+4:d(4)+3));
a10 = str2num(tline2(d(4)+4:d(5)+3));
tline3 = fgetl(f);
e = findstr(tline3,'E');
a11 = str2num(tline3(1:e(1)+3));
a12 = str2num(tline3(e(1)+4:e(2)+3));
a13 = str2num(tline3(e(2)+4:e(3)+3));
a14 = str2num(tline3(e(3)+4:e(4)+3));
%Creating an array of temperature points
LT = linspace(low_temp,mid_temp,50);
HT = linspace(mid_temp,high_temp,50);
%Finding Cp, H, s values at high temperature range
for i = 1:length(HT)
Cp_h(i) = 8.314*(a1 + a2*HT(i) + a3*HT(i)^2 + a4*HT(i)^3 + a5*HT(i)^4);
H_h(i) = 8.314*(a1*HT(i) + ((a2*HT(i)^2)/2) + ((a3*HT(i)^3)/3) + ((a4*HT(i)^4)/4) + ((a5*HT(i)^5)/5) + a6);
S_h(i) = 8.314*(a1*log(HT(i)) + a2*HT(i) + ((a3*HT(i)^2)/2) + ((a4*HT(i)^3)/3) + ((a5*HT(i)^4)/4) + a7);
end
%Finding Cp, H, s values at low temperature range
for j = 1:length(LT)
Cp_l(j) = 8.314*(a8 + a9*LT(j) + a10*LT(j)^2 + a11*LT(j)^3 + a12*LT(j)^4);
H_l(j) = 8.314*(a8*LT(j) + ((a9*LT(j)^2)/2) + ((a10*LT(j)^3)/3) + ((a11*LT(j)^4)/4) + ((a12*LT(j)^5)/5) + a13);
S_l(j) = 8.314*(a8*log(LT(j)) + a9*LT(j) + ((a10*LT(j)^2)/2) + ((a11*LT(j)^3)/3) + ((a12*LT(j)^4)/4) + a14);
end
%Plotting Cp,H,s vs temperature
figure(1)
plot(LT,Cp_l,'color','r')
hold on
plot(HT,Cp_h,'color','r')
xlabel('Temperature (deg C)');
ylabel('Specific Heat (J/kg K)');
title(species_name);
hold off
figure(2)
plot(LT,H_l,'color','r')
hold on
plot(HT,H_h,'color','r')
xlabel('Temperature (deg C)');
ylabel('Enthalpy (J)');
title(species_name);
hold off
figure(3)
plot(LT,S_l,'color','r')
hold on
plot(HT,S_h,'color','r')
xlabel('Temperature (deg C)');
ylabel('Entropy (J/K)');
title(species_name);
hold off
%Saving each figure in a seprate folder named as per the species name
mkdir('G:\Rohit\Online Courses\MATLAB for Mechanical Engineers\Answers\File Parsing\Species',species_name);
directory = (['G:\Rohit\Online Courses\MATLAB for Mechanical Engineers\Answers\File Parsing\Species\',species_name]);
cd(directory);
saveas(1,'Specific_heat.jpg');
saveas(2,'Enthalpy.jpg');
saveas(3,'Entropy.jpg');
end
Results:
1. Molecular weight results in output window
2. Screenshot of window with the result folders
3. Selected output plots
a) O2
b) N2
c) CO2
References:
MATLAB Online Help manuals