Project 1 - Parsing NASA thermodynamic data
AIM :
To write a code in MATLAB for parsing the NASA Thermodynamic data file and calculate specific data, enthalpy, entropy and molecular mass of each species by using the data.
FILE PARSING : Parsing a file means reading in a data stream and building a memory model of the content of that data. This aims at facilitating performing some kind of transformation on the data. Parsing splits a file or other input into pieces of data that can be easily stored or manipulated.
GOVERNING EQUATIONS :
1) Specific heat : Specific Heat Capacity is defined as the heat required to raise the temperature of a given mass by a unit temperature. It is denoted as Cp. its unit is J/K
`(CP)/R= a1+(a2*T)+(a3*T^2 )+(a4*T^3 )+(a5*T^4)`
2)Enthalpy : Enthalpy refers to the internal energy of a system which includes the temperature and pressure in an enclosed system, the change in heat is referred to as Enthalpy of the system. It is denoted as “H”. its unit is J/mol
`H/(RT)=a1+a2*T/2+a3*T^2/3+a4*T^3/4+a5*T^4/5+a6/T`
3)Entropy : Entropy is a function of state of a system. It is an external property and is reversible. Entropy plays and important part in thermodynamics. The second law of thermodynamics is based on entropy which says that entropy of an isolated system never decreases. It is denoted as “S” . Its unit is J/K.
`S/R=a1 lnT+a2*T+a3*T^2/2+a4*T^3/3+a5*T^4/4+a7`
Where,
T=local temperature
a1 to a7 = high temperature coefficients,
a8 to a14 = low temperature coefficients,
Cp = specific heat
H = enthalpy
S= entropy
R= Universal gas constant = 8.314 J/K*Mol
MATLAB code for parsing the NASA thermodynamic data file :
close all
clear all
clc
% To open the data file
f1 = fopen('THERMO.dat','r');
%Reading Heading line
Heading = fgetl(f1);
%Reading Global Temperature line
tglobal = fgetl(f1);
temp = strsplit(tglobal,' ');
global_low_temp = str2double(temp{2});
global_mid_temp = str2double(temp{3});
global_high_temp = str2double(temp{4});
%Reading comment lines
comment1 = fgetl(f1);
comment2 = fgetl(f1);
comment3 = fgetl(f1);
% To Extraxt coefficients of element and doing calculation
for i= 1:53
species_line = fgetl(f1);
species_name =strsplit(species_line,' ');
%Finding Coefficients for each element by reading each line
first_line= fgetl(f1);
a=findstr(first_line,'E');
second_line = fgetl(f1);
b=findstr(second_line,'E');
third_line= fgetl(f1);
c=findstr(third_line,'E');
%Coefficients are:-
C1 = str2num(first_line(1:a(1)+3));
C2 = str2num(first_line(a(1)+4:a(2)+3));
C3 = str2num(first_line(a(2)+4:a(3)+3));
C4 = str2num(first_line(a(3)+4:a(4)+3));
C5 = str2num(first_line(a(4)+4:a(5)+3));
C6 = str2num(second_line(1:b(1)+3));
C7 = str2num(second_line(b(1)+4:b(2)+3));
C8 = str2num(second_line(b(2)+4:b(3)+3));
C9 = str2num(second_line(b(3)+4:b(4)+3));
C10 = str2num(second_line(b(4)+4:b(5)+3));
C11 = str2num(third_line(1:c(1)+3));
C12 = str2num(third_line(c(1)+4:c(2)+3));
C13 = str2num(third_line(c(2)+4:c(3)+3));
C14 = str2num(third_line(c(3)+4:c(4)+3));
%Universal gas constant
R = 8.314;
%Extracting local temperatures for each Species line
local_low_temp=str2double(species_name{end-3});
local_mid_temp=str2double(species_name{end-1});
local_high_temp=str2double(species_name{end-2});
T = linspace(local_low_temp,local_high_temp,200);
%Calculating Cp,H,S
if T < local_mid_temp
Cp = R*(C8 + C9.*T + C10.*T^2 + C11.*T^3 + C12.*T^4);
H = (C8 + C9.*T/2 +(C10.*T.^2)/3 +(C11.*T.^3)/4 + (C12.*T.^4)/5 +C13./T)*R.*T;
S = (C8.*log(T) + C9.*T + (C10.*T.^2)/2+ (C11.*T.^3)/3 + (C12.*T.^4)/4 + C14)*R;
else
Cp = R*(C1 + C2.*T + C3.*T.^2 + C4.*T.^3 + C5.*T.^4);
H = (C1 + (C2.*T)/2 +(C3.*T.^2)/3 +(C4.*T.^3)/4 + (C5.*T.^4)/5 +C6./T)*R.*T;
S = (C1.*log(T) + C2.*T + (C3.*T.^2)/2+ (C4.*T.^3)/3 + (C5.*T.^4)/4 + C7)*R;
end
% calculating molecular weight of each
str = cell2mat(species_name(1));
s = ["H" "O" "C" "N" "A"];
atomic_weight = [1,16,12,14,40];
Mol_Weight=0;
for j=1:length(str);
for k=1:length(atomic_weight)
if strcmp(str(j),s(k));
Mol_Weight = Mol_Weight + atomic_weight(k);
position = k;
end
end
number = str2double(str(j));
if number > 1
Mol_Weight = Mol_Weight + atomic_weight(position)*(number-1);
end
end
M=Mol_Weight;
fprintf("The molecular weight of %s is %d g/mol \n",str,M)
%Providing path for saving data
targetdirectory = '/Users/srinu/Documents/MATLAB/NASA_data_parsing_results';
foldername = fullfile(targetdirectory, sprintf('%s',str));
mkdir(foldername);
%Going in to the folder of the species to save the plots
cd(foldername)
%Plotting
figure(1)
plot(T,Cp)
title(['Specific heat vs temperature plot for ',str])
xlabel('Temperature')
ylabel('Specific heat')
saveas(gcf, 'Cp.png');
figure(2)
plot(T,H)
title(['Enthalpy vs temperature plot for ',str])
xlabel('Temperature')
ylabel('Enthalpy')
saveas(gcf, 'H.png');
figure(3)
plot(T,S)
title(['Entropy vs temperature plot for ',str])
xlabel('Temperature')
ylabel('Entropy')
saveas(gcf, 'S.png');
cd ..
end
Explination :
- To open the NASA thermodynamic data fie, we use a command ‘fopen() and in the brackets we will choose whether we want to read or write the file by using ‘r’ or ‘w’ commands.
- Here we want to read the file so we use ‘fopen(filename,’r’) command File name is “THERMO.dat” and the data is assigned to a variable called f1
- Now to read the first line of the file we use a command ‘fgetl()’. This command used to read the individual line of the file and upon repeating the command it will read the next line in the sequence (or) in the data file.
- We read the first line of the NASA data file by using ‘fgetl’ command and it is assigned to a variable called ‘Heading’.
- To read the next line I,e., global temperature line in the data file we again used ‘fgetl’ command and assigned it to a variable ‘tglobal’
- Whenever we use ‘fgetl’ command it gives output in string, To separate the values in the line we use ‘strsplit()’ command and in the code we splitted the values in the tglobal by using the space in-between the values. After executing it will be converted into an array which is assigned to a variable ‘temp’.
- Still the values are characters not numbers, so convert them into number array by using a command ‘str2num()’, which converts a string value to numbers.in the str2num we have given the position of these characters and now assigning them to variables of ‘global_low_temp’, ‘global_mid_temp’ and ’global_high_temp’ respectively.
- We entered ‘fgetl()’ command three times to read the next the lines in the NASA data file which was comments and assigned them to three variables as ‘comment1’,comment2’ and ‘comment3’
- Now the information regarding all 53 species was extracted by using FOR loop
- Now for extracting the coefficients of species ‘fgetl()’ command is used , and the line read in the data file is assigned to a varaiable ‘element_line’, then by using ‘strsplit()’ the read line is splitted and the values are assigned to ‘element_name’
- To read the coefficients in the data file again ‘fgetl()’ is used.
- Now to get the coefficients from the data file we use ‘fget’()’ command and assigneed the three lines of coefficients to three varabiles as ‘first_line’, ‘second_line’, ‘third_line’.
- In the three line we find a common string ‘E’ and by using this string position we will find the individual coefficients
- By using ‘findstr()’ we find the position of string ‘E’ in the lines and assigned to a variables ‘a’,’b’,’c’ .
- By using the position of ‘E’ and by following the logic shown in the code we find the individual coefficients.
- Since we used FOR loop ,every time loop executed and find the coefficients of each elements and variables are assigned accordingly for all 53 species.
- Universal gas constant is assigned to a variable R.
- For each species in the data file there are separate local temperatures. These temperature ranges are used to calculate specific heat, enthalpy, entropy.
- To extract the temperature values, we used the same process that to extract global temperature values.
- To get the local temperatures we have used command ‘str2double()’ command instead of ‘str2num()’ command, this ‘str2double’ command converts the string in the cell array of string to double precision. Its inputs are same that of ‘str2num’command.
- Now we used the ‘if’ statement to find specific heat, enthalpy and entropy at low temperatures and high temperatures. Since first 7 coefficients are high temperature coefficients and next 7 coefficients are low temperature coefficients.
- So ‘if’ statement executes first set formulas if temperature are less than local mid temperature and if temperature is higher than it executes second set of formulas which was shown in the code.
- Formulas for specific heat, enthalpy, entropy are assigned to variables ‘CP’, ‘H’ , ‘S’ respectively.
- To calculate molecular weight of each species, first we converted the name of the species into matrix using ‘cell2mat()’ command. this command converts cell into an ordinary array.
- In NASA data file every element comprises of carbon(C), oxygen(O), nitrogen(N), hydrogen(H) and argon(A). so using these elements we will compare all the species with them and see what are the elements that each species consists of and then adding up atomic weight of each element to find molecular weight of the species.
- So first we have created a string that comprises of above elements first letter. It is assigned to a variable ‘s’. then an array is created comprising of each of the above element atomic mass values. this array is assigned to a variable ‘atomic_weight’.
- A variable ‘Mol_weight’ is assigned and its intial value is given 0. then a FOR loop is used in which the number of characters in a species name is taken as length and then againin another FOR loop length of ‘atomic_weight’ array is taken and for each value of ‘str’, each value of atomic weight is taken.
- An ‘if; statement is used with a command ‘strcmp()’ which compares the characters entered in it as input and if both the characters match with each other then it gives output as 1 and then it allows the next line to execute in the code and if the character does not match ‘if statement will stop and go back to repeat the FOR loop with new value.
- If the ‘if’ statement is satisfied then the atomic weight is added to the initial molecular weight and new molecular weight is obtained. now this molecular weight is the initial molecular weight for the next iteration in the FOR loop.
- Another ‘if’ statement is used to calculate the duplicate element if present in the species. When same element with more than one time is present then the number comes into iteration, then first ‘if; statement will pass to the second ‘if’ statement.
- Before going to second ‘if’ statement this number is a character datatype so it is changed to number using’str2double()’. This is assigned to variable number.after converting the ‘if’ statement is checked and if the statement is satisfied ‘mol_weight’ is further calculated by using the logic shown in code and stored the new molecular weight and is the final molecular weight value of that species.
- Final molecular weight is now assigned to a variable ‘M’.
- Then ‘fprintf()’ command is used to print a text statement in the command window that tell the molecular weight of each species.
- To save the graphs a path where the graphs are to be saved is entered and then ‘fulfill()’ command is used to save a folder with specified name and its path is given as input.
- ‘mkdir(foldername)’ command is used to create the directory ‘foldername’ in the current directory. Then using command ‘cd(foldername)’ the current working directory is set to directory that we opened using ‘mkdir’.
- Plotting is done using ‘plot()’ command.
- Title and labels are given to the plot.
- Each plot is separated by using ‘figure ()’ command and saved each plot by using ‘saveas()’ command.
- ‘cd..’ is used to close the opened folder. thus creating each folder for each species and each species having three graphs in their individual folder which was shown in the screenshot.
Error faced whle coding :
after execution of the code i got the result as molecular weight of all species in a single line which was shown in error screenshot
Steps taken to overcome eror : after adding \n in the 'fprintf()' command as shown below i got the result
Reference :
- https://in.mathworks.com/help/matlab/ref/mkdir.html
- one to one session