Project 1 - Parsing NASA thermodynamic data
clcclear allclose all
% Opening 'Thermo.dat' file in read mode using fopen function
ff1 = fopen('Thermo.dat','r')
% Titlel1 = fgetl(ff1);
% Global temperature valuesl2 = fgetl(ff1)A = strsplit(l2,' ');Global_min_Temp = str2num(A{2});Global_mid_Temp = str2num(A{3});Global_max_Temp = str2num(A{4});
% Ignoring next three commentsl3 = fgetl(ff1);l4 = fgetl(ff1);l5 = fgetl(ff1);
% Loop for covering all 53 species present in data file for i = 1:53 species = fgetl(ff1); species_name = species(1:6)
Lin_1 = fgetl(ff1); L1 = strfind(Lin_1,'E'); Cof1 = str2num(Lin_1(1:(L1(1)+3))); Cof2 = str2num(Lin_1((L1(1)+4):(L1(2)+3))); Cof3 = str2num(Lin_1((L1(2)+4):(L1(3)+3))); Cof4 = str2num(Lin_1((L1(3)+4):(L1(4)+3))); Cof5 = str2num(Lin_1((L1(4)+4):(L1(5)+3)));
Line_2 = fgetl(ff1); L2 = strfind(Line_2,'E'); Cof6 = str2num(Line_2(1:(L2(1)+3))); Cof7 = str2num(Line_2((L2(1)+4):(L2(2)+3))); Cof8 = str2num(Line_2((L2(2)+4):(L2(3)+3))); Cof9 = str2num(Line_2((L2(3)+4):(L2(4)+3))); Cof10 = str2num(Line_2((L2(4)+4):(L2(5)+3)));
Line_3 = fgetl(ff1); L3 = strfind(Line_3,'E'); Cof11 = str2num(Line_3(1:(L3(1)+3))); Cof12 = str2num(Line_3((L3(1)+4):(L3(2)+3))); Cof13 = str2num(Line_3((L3(2)+4):(L3(3)+3))); Cof14 = str2num(Line_3((L3(3)+4):(L3(4)+3)));
% Temperature Range assumedTemp = linspace(300,5000,500);
% Universal gas constantR = 8.314;
% Calculation using given formulafor j = 1:length(Temp) if Temp(j)>1000Cp = (Cof1+Cof2.*Temp+Cof3.*Temp.^2+Cof4.*Temp.^3+Cof5.*Temp.^4).*R;H = (Cof1+(Cof2.*Temp)./2+(Cof3.*Temp.^2)/3+(Cof4.*Temp.^3)/4+(Cof5.*Temp.^4/5)+Cof6./Temp).*Temp.*R;S = (Cof1*log(Temp)+Cof2.*Temp+(Cof3.*Temp.^2)/2+(Cof4.*Temp.^3)/3+(Cof5.*Temp.^4)/4+Cof7).*R; elseCp = (Cof8+Cof9.*Temp+Cof10.*Temp.^2+Cof11.*Temp.^3+Cof12.*Temp.^4).*R;H = (Cof8+(Cof9.*Temp)/2+(Cof10.*Temp.^2)/3+(Cof11.*Temp.^3)/4+(Cof12.*Temp.^4/5)+Cof13./Temp).*Temp.*R;S = (Cof8.*log(Temp)+Cof9.*Temp+(Cof10.*Temp.^2)/2+(Cof11.*Temp.^3)/3+(Cof12.*Temp.^4)/4+Cof14).*R; endend
current_dir = pwd; % 'current_dir' variable contains the path of current directorymkdir(species_name); % new directory is made using mkdir function with species namecd(species_name); % Moving to new species directory
%Plotting all graph
% Temp vs Entropyfigure(1)plot(Temp,S,'linewidth',3,'color','b')title(species_name)xlabel('Temperature')ylabel('Entropy')grid on%Temp vs Enthalpyfigure(2)plot(Temp,H,'linewidth',3,'color','g')title(species_name)xlabel('Temperature')ylabel('Enthalpy')grid on% Temp vs Specific heatfigure(3)plot(Temp,Cp,'linewidth',3,'color','r')title(species_name)xlabel('Temperature')ylabel('Specific heat')grid on%Saving the plot generated in species directory
saveas(figure(1), 'Entropy.jpg');saveas(figure(2), 'Enthalpy.jpg');saveas(figure(3), 'Specific heat.jpg');cd(current_dir);
Mass = molecular_mass(species_name) % Function for calculating molecular_mass
end
fclose(ff1) % Closing the Thermo.dat file
Plot of Enthalpy, Entropy, and Specific heat for O2, N2 and CO2:-
Folder where these plots are saved
Molecular Weight of Species
Explanation of Code
The file ‘Thermo.dat’ is open using in-built MatLab function ‘fopen’ in read mode. This thermo.dat file is assigned to a variable ff1. The fgetl function is used to read the file line by line. Firstly the title of this file is assigned to l1 variable. The second line has global maximum, minimum, mean temperature value that converted into a number using str2num command. The next three consecutive lines that contain comments are not required in our program, hence these lines are skipped. An array of five hundred temperature values range between 300 degrees to 5000 degrees is created using linspace command. The value of the universal gas constant is given as input in the code. ‘For’ loops are used to read the data of all species from the given file. The fgetl command is used to read file line by line. The coefficients are converted into the numeric form using str2num command and are stored in variables named Cof1 to Cof12. The first seven coefficients are high-temperature value and the next seven are low-temperature values. Now, using ‘if-else’ statement the assumed temperature range is divided into two parts i.e if temperature values are less than 1000 degree then the expression of entropy, enthalpy and specific heat is calculated based on low-temperature coefficient value, else are calculated based on high-temperature value coefficient for each species present in ‘thermo.dat’ file. A variable is created to store the path of the current working directory using pwd command. The command mkdir is used to create the directory for each species with their names. Now, using the cd command current directory is changed to a new-formed directory with the name of species. In this new directory, for each species, their variations in specific heat, enthalpy and entropy with temperature are plotted respectively and save using save as command. A separate molecular_species function is coded to get mass of each species.
Reference: NIST Chemistry WebBook