Project 1 - Parsing NASA thermodynamic data
AIM : To parse the NASA Thermodynamic data and managing it for necessary data accumulation using MATLAB.
THEORY :
- Parsing a file is reading in a data stream of some sort and building an in memory model of the semantic content of that data. This aims at perfoming some kind of transformation on the data as per need.
- To parse data in MATLAB we need to open & then read a file and perform various operations on data as per output needed.
- In MATLAB the most important commands used are fopen,fclose, fgetl, strsplit,str2double, etc in program code to parse the data file successfully.
- Functions of main commands used are –
- fopen – To open file or obtain information about open files.
Syntax used – fileID = fopen(filename)
- fclose- To close one or all files
Syntax used – fclose(fileID)
- fgetl – To read line from file or removing newline characters.
Syntax used – tline = fgetl(fileID)
- strsplit – To split string or character vector at specified delimiter
Syntax used – A = strsplit(str,delimiter)
- str2double – To convert strings to double precision values
Syntax used – A = str2double(str)
- GOVERNING EQUATIONS :
- The main equations used for calculating the thermodynamic properties are :
`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)*R`
- OBJECTIVES OF THE PROJECT:
- To write a function that extracts all the 14 coefficients and calculate the Specific heat (Cp) , Enthalpy (H) and Entropy (S) for all species from the data file.
- Calculate the Molecular weight of each species and show it in command window.
- Create the plots for Specific heat, Enthalpy and Entropy for the local temperature range from low to high temperature specific for each species.
- To save the plots as images with appropriate names and store them in separate folders for each species with suitable name.
- FUNCTION CODE FOR CALCULATING Cp,H,S -
function [Cp,H,S] = Thermo_Coeff(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T)
R = 8.314;
for k = 1:length(T)
if T(k) < 300
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).*R ;
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).*R ;
end
end
end
- FUNCTION CODE FOR CALCULATING MOLECULAR MASS -
function Molecular_Weight = Molecular_mass(Name_of_Species)
Species_No = upper(Name_of_Species);
Elements = {'H','C','N','O','AR'};
Atomic_Weight = [1 12 14 16 40];
Molecular_Weight = 0;
for i = 1 : length(Species_No)
for j = 1 : length(Elements)
if strcmp(Species_No(i),Elements(j))
Molecular_Weight = Molecular_Weight + Atomic_Weight(j);
e = j;
end
end
N = str2double(Species_No(i));
if N > 1
Molecular_Weight = Molecular_Weight + Atomic_Weight(e)*(N-1);
end
end
fprintf('Molecular_Weight of %s = ',Name_of_Species)
fprintf('%d \n',Molecular_Weight)
end
- MAIN CODE -
%% CODE for Parsing NASA Thermodynamic data
clear all
close all
clc
% Opening & Reading the data file -
F1 = fopen('THERMO.dat','r'); % Using fopen command to open THERMO.dat file and saving it as pointer F1
% Reading the first Line :
Header_line = fgetl(F1); % Reading the Header line using fgetl command
% Assigning the Temperatures for next Lines in DATA file -
Temp_line = fgetl(F1); % Reading the Header line using fgetl command
A = strsplit(Temp_line,' '); % Splitting the string in a line using strsplit command
% Defining the Global Temperatures
Global_low_Temp = str2double(A{2}); % Storing the Global low Tempature
Global_mid_Temp = str2double(A{3}); % Storing the Global mid Tempature
Global_high_Temp = str2double(A{4}); % Storing the Global high Tempature
% Avoiding the comments in next lines
for i = (1:3)
fgetl(F1); % Skipping the 3 comment lines
end
% Reading the next main lines for species :
for j = 1:53
Species = fgetl(F1); % Reading the species line
Name_of_Species = Species(1:6);
% For Reading the coefficients
% Reading line 1 & all 5 coefficients from line 1
Line_1 = fgetl(F1); % Reading the 1st species line and saving it as Line_1
a = strfind(Line_1,'E'); % Finding the string 'E' from Line_1 and saving it in Variable a
a1 = str2double(Line_1(1:(a(1)+3))); % Extracting the 1st coefficient,converting it in to precision no. and storing it as a1
a2 = str2double(Line_1((a(1)+4):(a(2)+3))); % Extracting the 2nd coefficient,converting it in to precision no. and storing it as a2
a3 = str2double(Line_1((a(2)+4):(a(3)+3))); % Extracting the 3rd coefficient,converting it in to precision no. and storing it as a3
a4 = str2double(Line_1((a(3)+4):(a(4)+3))); % Extracting the 4th coefficient,converting it in to precision no. and storing it as a4
a5 = str2double(Line_1((a(4)+4):(a(5)+3))); % Extracting the 5th coefficient,converting it in to precision no. and storing it as a5
% Reading line 2 & all 5 coefficients from line 2
Line_2 = fgetl(F1);
b = strfind(Line_2,'E'); % Finding the string 'E' from Line_2 and saving it in Variable b
a6 = str2double(Line_2(1:(b(1)+3))); % Extracting the 6th coefficient,converting it in to precision no. and storing it as a6
a7 = str2double(Line_2((b(1)+4):(b(2)+3))); % Extracting the 7th coefficient,converting it in to precision no. and storing it as a7
a8 = str2double(Line_2((b(2)+4):(b(3)+3))); % Similarly, extracting the 8th coefficient & storing it as a8
a9 = str2double(Line_2((b(3)+4):(b(4)+3))); % Similarly, extracting the 9th coefficient & storing it as a9
a10 = str2double(Line_2((b(4)+4):(b(5)+3)));
% Reading line 3 & all 4 coefficients from line 3
Line_3 = fgetl(F1);
c = strfind(Line_3,'E'); % Finding the string 'E' from Line_3 and saving it in Variable c
a11 = str2double(Line_3(1:(c(1)+3))); % Extracting the 11th coefficient & storing it as a11
a12= str2double(Line_3((c(1)+4):(c(2)+3)));
a13= str2double(Line_3((c(2)+4):(c(3)+3)));
a14= str2double(Line_3((c(3)+4):(c(4)+3))); % Similarly, extracting the last 14th coefficient & saving it as a14
% Creating the new folder for all species
mkdir(['C:\Users\ashutosh\Desktop\MATLAB PRAC\Parsing Project\Nasa Project-',Name_of_Species]);
T = linspace(Global_low_Temp,Global_high_Temp,500);
[Cp,H,S] = Thermo_Coeff(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T); % Calling the coefficient function
% Plots for Thermodynamic properties for species
% Plotting 1st for Specific heat & Temperature
figure(1)
plot(T,Cp,'linewidth',3,'color','r') % Plotting for Cp vs Temperature & giving color as red
xlabel('TEMPERATURE [T] - (k)')
ylabel('SPECIFIC HEAT [Cp] - (KJ)')
title(Name_of_Species) % Giving title for a plot
legend('Specific Heat(Cp) vs Temperature(T)')
% Plotting 2nd for Enthalpy & Temperature
figure(2)
plot(T,H,'linewidth',3,'color','b') % Plotting for H vs Temperature & giving color as blue
xlabel('TEMPERATURE [T] - (k)') % Labelling X axis
ylabel('ENTHALPY [H] - (KJ/mol)') % Labelling Y axis
title(Name_of_Species)
legend('Enthalpy(H) vs Temperature(T)')
% Plotting 3rd for Entropy & Temperature
figure(3)
plot(T,S,'linewidth',3,'color','g') % Plotting for S vs Temperature & giving color as green
xlabel('TEMPERATURE [T] - (k)') % Labelling X axis
ylabel('ENTROPY [S] - (KJ/mol.k)') % Labelling Y axis
title(Name_of_Species) % Giving title for a plot
legend('Entropy(S) vs Temperature(T)')
directory = (['C:\Users\ashutosh\Desktop\MATLAB PRAC\Parsing Project\Nasa Project-',Name_of_Species]);
cd(directory); % Changing the File directory to save plots
% Saving the Output plots
saveas(1,'SpecificHeat_Cp.png') % Saving 1st figure for SpecificHeat using saveas command
saveas(2,'Enthalpy_H.png') % Saving 2nd figure for Enthalpy using saveas command
saveas(3,'Entropy_S.png') % Saving 3rd figure for Entropy using saveas command
% Again Changing back the Directory
cd('C:\Users\ashutosh\Desktop\MATLAB PRAC\Parsing Project\Nasa Project-\')
Molecular_Weight = Molecular_mass(Name_of_Species); % Calling the Molecular_Weight function
end
fclose(F1) % Closing the data file by using fclose command
OUTPUTS :
- CALCULATED MOLECULAR WEIGHT -
- The above output shows the result for calculated final values for the molecular weight of all the different species in the command window.
- OUTPUTS FOR PLOTS-
- PLOTS for O2 -
- The above plot gives the results for O2 species for temperature vs specific heat(Cp) over the range of temperature.
- The above plot gives the results for O2 species for temperature vs Enthalpy(H) over the range of temperature.
- The above plot gives the results for O2 for temperature vs Entropy(S) over the range of temperature.
- PLOTS for N2 -
- The above plot gives the results for N2 species for temperature vs specific heat(Cp) over the range of temperature.
- The above plot gives the results for N2 for temperature vs Enthalpy(H) over the range of temperature.
- The above plot gives the results for N2 for temperature vs Entropy(S) over the range of temperature.
- PLOTS for CO2 -
- The above plot gives the results for CO2 species for temperature vs specific heat(Cp) over the range of temperature.
- The above plot gives the results for CO2 for temperature vs Enthalpy(H) over the range of temperature.
- The above plot gives the results for CO2 species for temperature vs Entropy(S) over the range of temperature.
- OUTPUT for all folders saved -
- The above image shows the Output for windows location where all the folders are created & saved for all 53 species .
- STEPWISE EXPLANATION OF THE CODE :
- We start the program by clearing the window and previous plots by using basic commands clear all ; close all ; clc.
- In main code first we open the file named ‘THERMO.dat’ using fopen command and giving the input as filename, the permission i.e to read by letter “r” and storing it as a pointer in variable F1.
- Then we read the header line that is the first line of the first by using fgetl command by giving its input as a pointer F1(fileID).
- Then we read the next line which represents the main Global temperature values for the species. We use same command fgetl to read the line and then split the string for getting separate temperatures from that line using strsplit command in MATLAB.
- After using strsplit command and saving the cell array values in variable A we converted strings to double precision values i.e in numbers using str2double command and store it as specific values for Global low, mid and high temperatures.
- Next 3 lines there are comments which we can avoid using fgetl command and implementing for loop by giving 3 iterations in which fgetl command is given which then skip comments.
- Then we are reading the main lines for species by using for loop for 53 iterations because there are 53 species in the data file to read and extract the coefficients and uselfull data.
- In for loop the 1st statements which we use is to get the species line and then species name. We are going to read the lines 1 by 1 using fgetl command and extract the coefficients.
- After reading the species line we find the letter E and its position in the line by using strfind command which is used to find the strings with other strings and save it in a variable a.
- Then again we use str2double command to convert the strings. Here we use the logic in syntax as str2double(Line_1(1:(a(1)+3))) which means in the line 1 for species take the digit from 1st position to a(1) means 1 parameter of the variable (a) as defined before which locates E positon, + 3 to add till the last digit of coefficient 1.
- Same above logic is used for the 2nd , 3rd and rest of the coefficients till all 14 coefficients are extracted and then we store it in different variables of certain lines as a1,a2,a3,a4,…… so on till a14 respectively.
- Next we have made a new folder using mkrdir command in MATLAB for saving all the species and its needed data easily by giving the location and path.
- Now, we had given the local temperature range which is unique for individual species and saved it in a variable T which is also used for calculation of enthalpy, entropy and Specific heat.
- Then created a function in a separate new script the function named as Thermo_Coeff which is created to calculate the Specific heat (Cp) , Enthalpy (H) and Entropy (S) for all the species in which also for loop and if else condition is used and saved it as same name for Thermo_Coeff
- Also, a another function is created named Molecular_mass in which we had calculated the molecular weight of the each species in which for loop and if conditions are used logically in function code and fprintf command is used to print the calculated values for molecular weight of all the species.
- Next , in main code after specifing the local temp. values in T we call our Thermo_Coeff function immediately to keep all the values of Cp,H and S in the loop.
- Then, we start plotting the Specific heat (Cp) , Enthalpy (H) and Entropy (S) for all the species against the Local temp. range 1 by 1.
- First we plot for Specific heat(Cp) & Temperature(T) by using plot command, giving the x and labels, title and legend and saving it as figure(1).
- Similarly, we had for Enthalpy & Temperature in figure 2, then Entropy & Temperature in figure 3 respectively.
- Next we had change the directory for the species file using cd command in MATLAB and also saved all the plots for different species for fig.1,2 & 3 respectively and call the Molecular_Weight function in the loop.
- Finally, we had close the data file after reading, extracting necessary parameters by using fclose(F1) syntax where fclose command closes the file successfully.
- Errors faced while programming :
- In use of function and changing the directory for folder in MATLAB as function file and new folder created location was different.
- Steps taken to overcome the Errors Faced –
- To avoid the error simple changed the location of function file and newfolder. Move all the files at same 1 location.
- Screenshots showing errors -
- CONCLUSION –
- Learned the concept and logic to parse the NASA data file in MATLAB. Hence, we had calculated the molecular weight, thermodynamic properties like the Specific heat (Cp) , Enthalpy (H) and Entropy (S) for various species & obtained the final plots for Cp, H & S against specific temperature range.
- REFERENCES -