Project 1 - Parsing NASA thermodynamic data
AIM: To read the NASA thermodynamic data and find the properties like specific heat, entropy and enthalpy for each species and also to plot these properties against temp. Also to find the molecular weight of the species respectively.
Implementation
The file to read is a .dat file and it is read using the matlab inbuilt functions using fopen, fgetl and fclose respectively. The data consists of the 14 co-efficients (7 for high temperature and 7 for low temperature) which are to read for each species seprately and find the thermodynamic properties of the species with respect to the local specias temperature range. Then found values are to plotted and saved in individual species files and also the molecular weight it to be found.
Initially the data file is read using the fopen('THERMO.dat','r') where r is for reading the data in the file. Then the global temperature are read using the fgetl as this functions in a sequence of indexing i.e. it reads the file line by line by advancing itself to the next line for every iteration on command. strsplit is used to break the read line into parts based on the reference given to divide and then str2num is used t convert the string to the number.
Now to read all the species the following operations for a single species are to be done in a loop so that all the data in the file is read and parsed.
Now to read the species name, after using the fgetl option, it is indexed to a certain value and then the blank spaces are removed by using the deblank option which converts the larger string with blanks to no blank string. The local temperatures are read and converted to the values from strings and then the range is defined for each species. Then the co-effients are read with the help of using findstr and indexed accordingly. Then the thermodynamoic properties are found by using the respective functions created based on the follwing formulaes.
The molecular weightis found by comparing the species name string to the common string of elements like H, O, N, Ar, C, S respectively and then if so matched the respecive weights are added with the respective number of times
Eg: H2O : wt=0; wt=0+1+16=17; then as 2>1 now wt=17+1*(2-1)=18.
Later, to save the plots for each species, in the current location of the code, a new folder is created with the then species name using mkdir(name) command and that folder is made as the current location by cd(name) command where the then made plots for the respective species are saved using the saveas(figure name, filename and format) command. Finally after doing so the location is bought back to the location of the codde save in order to run the code for the nect iteration of the loop.
Finally the molecular weight is printed using sprintf statement. So now the complete code for the single species is done and this is to repeated again for the next coming species in the data file. So for loop is used to do so. Then the after completion the data file is closed.
The following is the code for the above defined process.
clear all
close all
clc
%file opening of the THERMO.dat
f1=fopen('THERMO.dat','r');
line_1=fgetl(f1);
line_2=fgetl(f1);
%retrieving the global temp values
global_temp= strsplit(line_2,' ');
g_low_temp= str2num(global_temp{2});
g_mid_temp= str2num(global_temp{3});
g_high_temp= str2num(global_temp{4});
%skip the lines
line_3=fgetl(f1);
line_4=fgetl(f1);
line_5=fgetl(f1);
%Ris universal gas constant in KJ/Kg-mol-K
R=8.314;
for i=1:53
T=fgetl(f1);
%reading the species name
species_name=T(1:9);
species_name=deblank(species_name);
%reading the local temp of the species
species_temp=T(49:75);
local_temp=strsplit(species_temp,' ');
l_low_temp=str2num(local_temp{1});
l_mid_temp=str2num(local_temp{3});
l_high_temp=str2num(local_temp{2});
%creating a temp range for the local temps
ltemp=[l_low_temp l_mid_temp l_high_temp];
temp_range=linspace(l_low_temp,l_high_temp,500);
%reading the lines for retreiving the 14 coefficients
line6=fgetl(f1);
coeff1=findstr(line6,'E');
h_a1=line6(1:coeff1(1)+3);
h_a1=str2num(h_a1);
h_a2=line6(coeff1(1)+4:coeff1(2)+3);
h_a2=str2num(h_a2);
h_a3=line6(coeff1(2)+4:coeff1(3)+3);
h_a3=str2num(h_a3);
h_a4=line6(coeff1(3)+4:coeff1(4)+3);
h_a4=str2num(h_a4);
h_a5=line6(coeff1(4)+4:coeff1(5)+3);
h_a5=str2num(h_a5);
%next line
line7=fgetl(f1);
coeff2=findstr(line7,'E');
h_a6=line7(1:coeff2(1)+3);
h_a6=str2num(h_a6);
h_a7=line7(coeff2(1)+4:coeff2(2)+3);
h_a7=str2num(h_a7);
l_a1=line7(coeff2(2)+4:coeff2(3)+3);
l_a1=str2num(l_a1);
l_a2=line7(coeff2(3)+4:coeff2(4)+3);
l_a2=str2num(l_a2);
l_a3=line7(coeff2(4)+4:coeff2(5)+3);
l_a3=str2num(l_a3);
%next line
line8=fgetl(f1);
coeff3=findstr(line8,'E');
l_a4=line8(1:coeff3(1)+3);
l_a4=str2num(l_a4);
l_a5=line8(coeff3(1)+4:coeff3(2)+3);
l_a5=str2num(l_a5);
l_a6=line8(coeff3(2)+4:coeff3(3)+3);
l_a6=str2num(l_a6);
l_a7=line8(coeff3(3)+4:coeff3(4)+3);
l_a7=str2num(l_a7);
%completed reading 14 coeff i.e, 7 for high temp and 7 for low temp
%calculating specific heat
cp=specific_heat(h_a1,h_a2,h_a3,h_a4,h_a5,l_a1,l_a2,l_a3,l_a4,l_a5,ltemp,temp_range);
%calculating enthalpy
H=enthalpy(h_a1,h_a2,h_a3,h_a4,h_a5,h_a6,l_a1,l_a2,l_a3,l_a4,l_a5,l_a6,ltemp,temp_range);
%calulating entropy
s=entropy(h_a1,h_a2,h_a3,h_a4,h_a5,h_a7,l_a1,l_a2,l_a3,l_a4,l_a5,l_a7,ltemp,temp_range);
%calculation of molecular mass of the species.
wt=weight(species_name);
%saving in new folders
current_loc=pwd;
%creating a new folder in the existing location
mkdir(species_name);
cd(species_name);
%plot for temp vs cp
figure(1)
plot(temp_range,cp,'linewidth',2);
xlabel('temperature K');
ylabel('specific heat KJ/kg-K');
title(species_name);
%plot for temp vs H
figure(2)
plot(temp_range,H,'linewidth',2);
xlabel('temperature K');
ylabel('enthalpy KJ/kg');
title(species_name)
%plot for temp vs S
figure(3)
plot(temp_range,s,'linewidth',2);
xlabel('temperature K');
ylabel('entropy KJ/K');
title(species_name);
%saving the plots in the created folder
saveas(1,'specific heat.jpg');
saveas(2,'enthalpy.jpg');
saveas(3,'entropy.jpg');
%to get back to the original directory
cd(current_loc);
%print the molecualr wt of the species
sprintf('molecular weight of %s = %.3f',species_name,wt)
end
%file close
fclose(f1)
The following is the function for specific heat
function [cp]= specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,temp,temp_range)
%a1,a2,a3,a4,a5 are coeff for high temp
%a6,a7,a8,a9,a10 are coeff for low temp
R=8.314;
for i= 1:length(temp_range)
t=temp_range(i);
if t<=temp(2)
cp(i)=(a6+a7*t+a8*(t.^2)+a9*(t.^3)+a10*(t.^4))*R;
else
cp(i)=(a1+a2*t+a3*(t.^2)+a4*(t.^3)+a5*(t.^4))*R;
end
end
end
The following is the function for enthalpy
function [H]= enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,temp,temp_range)
R=8.314;
%a1,a2,a3,a4,a5,a6 are coeff for high temp
%a7,a8,a9,a10,a11,a12 are coeff for low temp
for i=1:length(temp_range)
t=temp_range(i);
if t<=temp(2)
H(i)=R*t*(a7+(a8*t)/2+(a9*t^2)/3+(a10*t^3)/4+(a11*t^4)/5+a12/t);
else
H(i)=R*t*(a1+(a2*t)/2+(a3*t^2)/3+(a4*t^3)/4+(a5*t^4)/5+a6/t);
end
end
end
The following is the function for entropy
function [s]= entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,temp,temp_range)
R=8.314;
%a1,a2,a3,a4,a5,a7 are coeff for high temp
%a8,a9,a10,a11,a12,a14 are coeff for low temp
for i=1:length(temp_range)
t=temp_range(i);
if t<=temp(2)
s(i)=R*(a8*log(t)+a9*t+(a10*t^2)/2+(a11*t^3)/3+(a12*t^4)/4+a14);
else
s(i)=R*(a1*log(t)+a2*t+(a3*t^2)/2+(a4*t^3)/3+(a5*t^4)/4+a7);
end
end
end
The following is the function for molecular weight
function[wt]= weight(species)
element= {'H','O','N','Ar','C','S'};
atomic_mass= [1, 16, 14, 39, 12, 32];
wt=0;
for i= 1:length(species)
for j= 1:length(element)
if strcmp(species(i),element(j))
wt= wt + atomic_mass(j);
a= j;
end
end
b= str2num(species(i));
if b>1
wt= wt + atomic_mass(a)*(b-1);
end
end
end
The above codes are used to find the thermodynamic properties and alsso the molecular weights of the species along with the plots made as per the requirement.
Outputs
The following are the screenshots showing the molecular weight of each species in the data file that is parsed.
The following is the screenshot showing the folders with the plots been saved in them, that were created by the code with the species name.
The following are the plots obtained by the above code for O2
The following are the plots obtained by the above code for N2
The following are the plots obtained by the above code for CO2
Errors:
The errors faced were of typo's and syntax errors where the parenthesis aren't closed.
Immediate correction was done as per the indications from the command window.
The following are the srceenshots showing the errors.
The following is the drive link for the created species folder and respective plots
https://drive.google.com/drive/folders/1D5BQ0SI3V0pTMuz3HHc5SxI3KqglJxYe?usp=sharing
Conclusion: The file is parsed and the evaluaton of the thermodynamic properties is done as per the above stated code and formulaes succesfully for each species and the plots made are saved seperately for each species in folder. The molecular weight of each species is also calculated.
References:
- https://in.mathworks.com/help/matlab/ref/strsplit.html
- https://in.mathworks.com/help/matlab/ref/sprintf.html
- https://in.mathworks.com/help/matlab/ref/str2num.html
- https://in.mathworks.com/help/matlab/ref/saveas.html
- https://in.mathworks.com/help/matlab/ref/mkdir.html
- https://in.mathworks.com/help/matlab/ref/cd.html
- https://in.mathworks.com/help/matlab/ref/deblank.html