Project 1 - Parsing NASA thermodynamic data
%Program to parse NASA thermodynamic data
close all
clear all
clc
%Reading the THERMO.dat file
Thermfile=fopen('THERMO.dat','r')
%Heading
line1=fgetl(Thermfile)
%Global temperature values
line2=fgetl(Thermfile)
%skipping the next three lines (comments)
cmt_1=fgetl(Thermfile)
cmt_2=fgetl(Thermfile)
cmt_3=fgetl(Thermfile)
%splitting the global temperature values
A=strsplit(line2,' ');
%storing each global temperature values
Global_low_temperature=str2num(A{2})
Global_mid_temperature=str2num(A{3})
Global_high_temperature=str2num(A{4})
%to get species information
for i=1:53
species= fgetl(Thermfile);
species_name=species(1:6)
Line_1=fgetl(Thermfile);
L1=strfind(Line_1,'E');
%storing each coefficient values
a1=str2num(Line_1(1:(L1(1)+3)));
a2=str2num(Line_1((L1(1)+4):(L1(2)+3)));
a3=str2num(Line_1((L1(2)+4):(L1(3)+3)));
a4=str2num(Line_1((L1(3)+4):(L1(4)+3)));
a5=str2num(Line_1((L1(4)+4):(L1(5)+3)));
Line_2=fgetl(Thermfile);
L2=strfind(Line_2,'E');
a6=str2num(Line_2(1:(L2(1)+3)));
a7=str2num(Line_2((L2(1)+4):(L2(2)+3)));
a8=str2num(Line_2((L2(2)+4):(L2(3)+3)));
a9=str2num(Line_2((L2(3)+4):(L2(4)+3)));
a10=str2num(Line_2((L2(4)+4):(L2(5)+3)));
Line_3=fgetl(Thermfile);
L3=strfind(Line_3,'E');
a11=str2num(Line_3(1:(L3(1)+3)));
a12=str2num(Line_3((L3(1)+4):(L3(2)+3)));
a13=str2num(Line_3((L3(2)+4):(L3(3)+3)));
a14=str2num(Line_3((L3(3)+4):(L3(4)+3)));
%creating folder for each species
mkdir(['C:\Users\vaish\File parsing NASA\Species\',species_name]);
%Temperature ranges for calculation
Temperature=linspace(300,5000,100);
%Universal gas constant
R=8.314;
%calculation of enthalpy,entropy and specific heats for all the species
for j = 1:length(Temperature)
if Temperature(j)<3000
Cp= (a1+a2.*Temperature+a3.*Temperature.^2+a4.*Temperature.^3+a5.*Temperature.^4).*R;
H= (a1+(a2.*Temperature)./2+(a3.*Temperature.^2)/3+(a4.*Temperature.^3)/4+(a5.*Temperature.^4/5)+a6./Temperature).*Temperature.*R;
S= (a1*log(Temperature)+a2.*Temperature+(a3.*Temperature.^2)/2+(a4.*Temperature.^3)/3+(a5.*Temperature.^4)/4+a7).*R;
else
Cp= (a8+a9.*Temperature+a10.*Temperature.^2+a11.*Temperature.^3+a12.*Temperature.^4).*R;
H= (a8+(a9.*Temperature)/2+(a10.*Temperature.^2)/3+(a11.*Temperature.^3)/4+(a12.*Temperature.^4/5)+a13./Temperature).*Temperature.*R;
S= (a8.*log(Temperature)+a9.*Temperature+(a10.*Temperature.^2)/2+(a11.*Temperature.^3)/3+(a12.*Temperature.^4)/4+a14).*R;
end
end
%plotting
%Temperature vs Entropy
figure(1)
plot(Temperature,S,'linewidth',4,'color','r')
title(species_name)
xlabel('Temperature')
ylabel('Entropy')
%Temperature vs Enthalpy
figure(2)
plot(Temperature,H,'linewidth',4,'color','b')
title(species_name)
xlabel('Temperature')
ylabel('Enthalpy')
%Temperature vs Specific heat
figure(3)
plot(Temperature,Cp,'linewidth',4,'color','g')
title(species_name)
xlabel('Temperature')
ylabel('Specific heat')
%changing the filepath to save the plots
directory = (['C:\Users\vaish\File parsing NASA\Species\',species_name]);
cd(directory)
%saving the plots
saveas(1,'Entropy.jpg');
saveas(2,'Enthalpy.jpg');
saveas(3,'Specific heat.jpg');
%switching the directory back to read thermodat file for molecular mass calculation
cd('C:\Users\vaish\File parsing NASA\')
MM = molecular_mass(species_name)
end
fclose(Thermfile)
%Function to calculate the molecular masses of individual species
function MM = molecular_mass(species_name)
species= upper(species_name);
Constituents= ['H', 'C', 'N', 'O', 'A'];
Atomic_weight= [1.00794 12.0107 14.0067 15.9994 39.948];
MM= 0;
for i=1:length(species)
for j=1:length(Constituents)
if strcmp(species(i),Constituents(j))
MM = MM+Atomic_weight(j);
u = j;
end
end
n = str2num(species(i));
if n>1
MM = MM+Atomic_weight(u)*(n-1);
end
end
end


PARSING NASA THERMODYNAMIC DATA
Step-Wise Explanation of Code
- First of all, I have read the given NASA thermodynamic data file using ‘fopen’ command and used ‘fgetl’ command to read each line of the data file.
- Then I have splitted the readed line by using ‘strsplit’ command to get global temperature values and stored them individually.
- To get species names, I have used ‘for’ loop and to get each coefficient values, I have used the string find method and specified the string to be find out by using ‘strfind’ command and stored these values separately.
- Then to create folder for each species, I have used ‘mkdir’ command and specified the directory.
- For the calculation purpose, temperature and universal gas constant values have been specified and calculated the enthalpy, entropy and specific heats for all the species.
- Then I have plotted the graphs of temperature vs entropy, temperature vs enthalpy and temperature vs specific heat.
- The to change the file path to save the plots, I have used the ‘cd’ command and specified the file directory. Then by again using ‘cd’ command, I have switched the directory back to read thermos.dat file for molecular mass calculation.
- To calculate molecular mass of a species, I have created a function, which will calculate molecular mass of each and every species and then the program is ended.
Errors Faced While Programming
I have faced an error while creating folder for each species by using ‘mkdir’ command and in changing the file path by ‘cd’ command.
Steps Taken to Overcome the Errors
The Matlab have showed the error while using the ‘mkdir’ and ‘cd’ command and I have resolved it by correctly following the syntax of ‘mkdir’ and ‘cd’ commands.
REFERENCES