Project 1 - Parsing NASA thermodynamic data
File parsing
Aim:
- To write a function to find the specific heat, entropy and enthalpy from a given data set
- Calculate the molecular weight of each species
- Plot the Cp, Enthalpy and Entropy for the local temperature range specific for each species.
Objective:
- Write a program to read an external data file name ‘Thermo.dat’ file
- Commands used to interpret different lines in the data file
- Interpret the name of species from the data file
- Calculate the enthalpy, entropy and specific heat of the species using 7 coefficients
- Plot graph of entropy, enthalpy and specific heat against temperature
- Save the plots in different directories with names of species.
Report
Thermodynamic properties of most of the species can be obtained using polynomial functions. This has been developed by the NASA. 14 coefficients are used to obtain the required variables.
clear all
close all
clc
f1=fopen('THERMO.dat','r');
fgetl(f1)f
A=fgetl(f1);
A=strsplit(A,' ');
global_low_temp=str2num(A{2});
global_mid_temp=str2num(A{3});
global_high_temp=str2num(A{4});
for i=1:3
fgetl(f1);
end
for i=1:53
header=fgetl(f1);
x=strsplit(header,' ');
species_name=(x{1});
z=length(x);
local_low_temp=str2double(x{z-3});
local_high_temp=str2double(x{z-2});
local_mid_temp=str2double(x{z-1});
t1=local_low_temp;
t2=local_mid_temp;
t3=local_high_temp;
line_1=fgetl(f1);
a=strfind(line_1,'E');
a1=str2double(line_1(1:a(1)+3));
a2=str2double(line_1(a(1)+4:a(2)+3));
a3=str2double(line_1(a(2)+4:a(3)+3));
a4=str2double(line_1(a(3)+4:a(4)+3));
a5=str2double(line_1(a(4)+4:a(5)+3));
line_2=fgetl(f1);
a_1=strfind(line_2,'E');
a6=str2num(line_2(1:a_1(1)+3));
a7=str2double(line_2(a_1(1)+4:a_1(2)+3));
a8=str2double(line_2(a_1(2)+4:a_1(3)+3));
a9=str2double(line_2(a_1(3)+4:a_1(4)+3));
a10=str2double(line_2(a_1(4)+4:a_1(5)+3));
line_3=fgetl(f1);
a_2=strfind(line_1,'E');
a11=str2double(line_3(1:a_2(1)+3));
a12=str2double(line_3(a_2(1)+4:a_1(2)+3));
a13=str2double(line_3(a_2(2)+4:a_1(3)+3));
a14=str2double(line_3(a_2(3)+4:a_1(4)+3));
mkdir(species_name);
T=linspace(t1,t3,1000);
R=8.314;
for k=1:length(T)
if T(k)>t2
cp=(a1+a2.*T+a3.*T.^2+a4.*T.^3+a5.*T.^4)*R;
H=(a1+(a2.*T)/2+(a3.*T.^2)/3+(a4.*T.^3)/4+(a5.*T.^4)/5+(a6./T))*R.*T;
S=a1.*log(T)+a2.*T+(a3.*T.^2)/2+(a4.*T.^3)/3+(a5.*T.^4)/4+a7;
else
cp=(a8+a9.*T+a10.*T.^2+a11.*T.^3+a12.*T.^4)*R;
H=(a8+(a9.*T)/2+(a10.*T.^2)/3+(a11.*T.^3)/4+(a12.*T.^4)/5+(a13./T))*R.*T;
S=a8.*log(T)+a9.*T+(a10.*T.^2)/2+(a11.*T.^3)/3+(a12.*T.^4)/4+a14;
end
end
figure(1);
plot(T,cp);
grid on ;
title(species_name);
xlabel('Temperature');
ylabel('Specific Heat');
figure(2);
plot(T,H);
grid on ;
title(species_name);
xlabel('Temperature');
ylabel('Enthalpy');
figure(3);
plot(T,S);
grid on ;
title(species_name);
xlabel('Temperature');
ylabel('Entropy');
cd (species_name)
saveas(1,'specific heat','jpg');
saveas(2,'enthalpy','jpg');
saveas(3,'entropy','jpg');
cd ..
elements=['H','C','O','N','A'];
atomic_weight=[1.0079 12.0107 15.999 14.0067 39.948];
mol_mass=0;
for l=1:length(species_name)
for m=1:length(atomic_weight)
if strcmp(species_name(l),elements(m))
mol_mass=mol_mass+atomic_weight(m);
position=m;
end
end
n=str2double(species_name(l));
if n>1
mol_mass=mol_mass+atomic_weight(position)*(n-1);
end
end
sprintf('Molecular Weight of %s:%f u',species_name,mol_mass)
end
fclose(f1)
To obtain the coefficients we initially read the file named ‘Thermo.dat’ from the given file. The files are read using the fopen command. The data in the die is stored into a variable named f1.
The image above shows that the thermo.dat file contains data. The header data contains value of string ‘THERMO’ which denotes the name of the file. Such lines are read only and serves no purpose on programming. The program can be routed from line to line using the command fgetl.
f1=fopen('THERMO.dat','r');
fgetl(f1)
Line 2 denotes the temperature ranges of low medium and high temperature ranges. The values are separated by a space and thus the string variable separated y spacing is stored into an array named A.
A=strsplit(A,' ');
Each variable of A is stored is the temperature ranges
global_low_temp=str2num(A{2});
global_mid_temp=str2num(A{3});
global_high_temp=str2num(A{4});
The different values of temperature which are stored as string variable is convertd into a number and stored into 3 variables named global_low_temp, global_medium_temp, global_high_temp which stores the low , medium and high temperature values.
Next 3 lines contains the NASA default memory for identification. These lines has to be skipped since it serve no purpose in the programming. The skipping of lines is done using the fgetl command.
for i=1:3
fgetl(f1);
end
From line 6 the programming begins by reading the name of the species. A total of 53 species are given n the dta file. To ead all those file a for loop has been created
for i=1:53
At every point of i a variable named ‘header, is used to save the header details of the species. Another array of x is used to split the header file to save each of the string variables into separate strings. Each strings are obtained based on the spaces between the strings.
header=fgetl(f1);
x=strsplit(header,' ');
Name of the species will be on the start of the header file. Hence x(1) will be the name of species. Another variable named species _name is used to store the species name. total stringsin the array x is counted using a function named length and is stored using a variable z.
species_name=(x{1});
z=length(x);
Each of the header file contains 3 temperature ranges of low, high and medium in the given order. The array value of x contains string type variable of these. The third last variable in the array x is the lowest local temperature. The string variable is converted into double integer using str2double command. Local_low_temp is the file name used to save the local low temperature. Similarly medium and high temperatures of the species is saves using variables naming local_high_temp and local_mid_temp.
local_low_temp=str2double(x{z-3});
local_high_temp=str2double(x{z-2});
local_mid_temp=str2double(x{z-1});
The values are then saved to another variables t1,t2,t3.
t1=local_low_temp;
t2=local_mid_temp;
t3=local_high_temp;
Each of the species header contains 3 lines and and in total contain 14 polynomials which can be used to find the entropy, enthalpy and specific heat based on the following relation. Command has been give to move to the next line using fgetl.
line_1=fgetl(f1);
As the programme reaches the first line of the pointer line we created an array named a. Array contains string variables where values of the polynomials are stores as constant. Each of the variables are found using the exponential varial ‘E’. variables of array array from 1st position to 3 variables after the exponential letter E is taken as a single string. The string is later converted into a double integer using str2double function. Similarly we find all the remaining 4 data values using similar commands
a=strfind(line_1,'E');
a1=str2double(line_1(1:a(1)+3));
a2=str2double(line_1(a(1)+4:a(2)+3));
a3=str2double(line_1(a(2)+4:a(3)+3));
a4=str2double(line_1(a(3)+4:a(4)+3));
a5=str2double(line_1(a(4)+4:a(5)+3));
Thus program is written for the remaining 2 lines of the same species. Till then the program has been done to read all the string variables in the data file and convert it into integer values so that calculation can be done.
- Programming to calculate the specific heat, entropy and enthalpy and plot the graph
In prior to that it was required to create a folder in the name of the species. Name of the species was stored using a variable called species_name from header name.
mkdir(species_name);
We have created a 1000 temperature range of value from t1 to t3 and assigned the values in an array called T
T=linspace(t1,t3,1000);
We need to assign the coefficient values on the basis if the temperature values used for the analysis. If the temperature value we use is greater than the medium temperature we use the first 7 polynomials whereas when the value of temperature is less than the mid temperature we make use of low temperature coefficients. We create an array of length of temperature array. And ‘if’ condition is used to check if the temperature value is greater or more than the mid temperature.
for k=1:length(T)
if T(k)>t2
In this case we have used if condition to check if the value of temperature T array is greater than the high temperature. If the condition satisfies we make use of the initial 7 polynomials named a1 to a7.
cp=(a1+a2.*T+a3.*T.^2+a4.*T.^3+a5.*T.^4)*R;
H=(a1+(a2.*T)/2+(a3.*T.^2)/3+(a4.*T.^3)/4+(a5.*T.^4)/5+(a6./T))*R.*T;
S=a1.*log(T)+a2.*T+(a3.*T.^2)/2+(a4.*T.^3)/3+(a5.*T.^4)/4+a7;
else
when the if condition fails to act we make use of else command and use the remaining 7 polynomials to finf the specific heat, enthalpy and entropy.
cp=(a8+a9.*T+a10.*T.^2+a11.*T.^3+a12.*T.^4)*R;
H=(a8+(a9.*T)/2+(a10.*T.^2)/3+(a11.*T.^3)/4+(a12.*T.^4)/5+(a13./T))*R.*T;
S=a8.*log(T)+a9.*T+(a10.*T.^2)/2+(a11.*T.^3)/3+(a12.*T.^4)/4+a14;
end
end
we close the both loop inside the if condition. On applying these conditions we have found the enthalpy, entropy and specific heat of the particular species named species_name. Once these values are obtained it is required to plot the entropy, enthalpy and specific heat against temperature. We plot the required graph and labeled it.
figure(1);
plot(T,cp);
figure(2);
plot(T,H);
figure(3);
plot(T,S);
Using cd command we can create a directory and save it in the name of species. Once the directory is created it is required to save the figure 1,2,3 into files with name of ‘specific heat’,’enthalpy’, ‘entropy’.
cd (species_name)
saveas(1,'specific heat','jpg');
saveas(2,'enthalpy','jpg');
saveas(3,'entropy','jpg');
After images are saved we go back to the root directory using the file name using cd .. command
- Finding the molecular mass of the species
In order to find the molecular mass of the species an array is created. Array contains name of element. H, C, O,N,Ar which represents the Hydrogen, Carbon, Oxygen, Nitrogen, Argon. Create another array of name atomic_weight which stores the value of the element.
elements=['H','C','O','N','Ar'];
atomic_weight=[1.0079 12.0107 15.999 14.0067 39.948];
Molecular mass of the species is equal to the sum of the molecular mass of the elements in the species. Create another variable to store the molecular mass named mol_mass and initialize the value to 0.
mol_mass=0;
Since we have 53 spcies , 53 molecular masses has to be found out. Inorder to find it we initialy create a foor loop and with length equal to the number of species. Create another for loop inside which we calculate the molecular mass of the species. Strcmp is a function name which correlate with the element name and the species name. when the species name is not found in the element list, it exit the loop. We also note the position of the loop with the second loop.
for l=1:length(species_name)
for m=1:length(atomic_weight)
if strcmp(species_name(l),elements(m))
mol_mass=mol_mass+atomic_weight(m);
position=m;
end
end
Whenever there are more than 2 atoms of single element we need to multiply the atomic number with the number of atoms. In order to do that we create another variable name n which converts the characters of the species into double format. Whenever the valueof n is greater than 1, it enters the if condition and multipies the value with the atomic mass of the corresponding values
n=str2double(species_name(l));
if n>1
mol_mass=mol_mass+atomic_weight(position)*(n-1);
end
end
After performing this operation we obtain the value of molecular mass of the particular species which required to be printed. It is done using the following command
sprintf('Molecular Weight of %s:%f u',species_name,mol_mass)
end
On running the above program we obtain the result by printing the plot of specific heat, enthalpy and entropy against temperature. It also create folder with the name of the species and within the folder has the plot for different species. Finally we have obtained the molecular mass of the required species.
Results
- The results of 53 species has been obtained on the required folder
Plot of O2- Entropy, Enthalpy and Specific heat
Entropy
Enthalpy
Specific Heat
Plot of N2- Entropy, Enthalpy and Specific
Entropy
Enthalpy
Specific Heat
Plot of CO2- Entropy, Enthalpy and Specific
Entropy
Enthalpy
Specific Heat