Project 1 - Parsing NASA thermodynamic data
Aim: To extract all the coefficients of the given thermodynamic data and to calculate the different parameters like Entropy, Enthalpy, and Specific Heat with respect to Temperature and also to calculate molecular mass of different species mentioned in the given le.
Theory: Parsing in computer languages refers to the syntactic analysis of the input code into its components parts in order to facilitate the writing of compilers and interpreters. It can be used to describe split or separation.
Parsing a le means reading in a data stream of some sort and building an in memory model of the semantic content of that data. This aims at facilitating performing some kind of transformation on the data.
Formula used:
Steps:
- Initially open and read the le using the ‘fopen’ command and then use the ‘fgetl’ command to read the data line by line.
- Since the first three lines are comment lines. Hence, we should use the ‘fgetl’ command on all three lines to read it as a comment line.
- Now we will use the ‘str2num’ command to convert a string scalar or character array to a numeric matrix.
- Now we will use ‘for’ loop to run the command 53 times to get the required data of different species. In this, we will also use ‘str2nd’ command to search the string inside another string.
- ‘str2num’ command is used again and again from C1 to C14 to convert a string scalar to a numeric matrix so that it can be used to nd the coefficients.
- Now we will use ‘mkdir’ command for creating a folder for the species we will get as output.
- Now we will use ‘linspace’ command for the temperature ranges from 300 to 2000 K using in 200 divisions.
- Now we will calculate the properties like Enthalpy, Entropy, and Species using the given formulae. At first we will use the ‘for’ loop to dene the range from 1 to temperature divisions which are 200.
- Now we will use ‘if else’ statement to calculate the properties if temperature<3000 and if it's not then else statement.
- In this step we will plot the different species for Enthalpy against Temperature, Entropy against Temperature, and Specific Heat against Temperature.
- Now we will save each and every le in the directory using the ‘saveas’ command and at last ‘cd’ command to display the current folder.
- After that, we will use the function ‘Molecular_mass’ to run all the species.
- Finally, we will use the ‘fclose’ command to close the specified file
MATLAB Code:
function MM = molecular_mass(species_name)
species = upper(species_name);
element = ['H', 'C', 'N', 'O', 'A'];
% Name of the elements
atomic_wt = [1.00794 12.0107 14.0067 15.9994 39.948];
%Molecular mass of elements
MM = 0;
%Initialize
for i=1:length(species)
for j=1:length(element)
if strcmp(species(i),element(j))
MM = MM + atomic_wt(j);
u = j;
end
end
n = str2num(species(i));
if n>1
MM = MM + atomic_wt(u)*(n-1);
end
end
end
close all
clear all
clc
%Opening & Reading of file
file = fopen('THERMO.dat','r')
%Reading of line
l1 = fgetl(file)
%Global temperature values
l2 = fgetl(file)
%Comment Lines
comm_1 = fgetl(file)
comm_2 = fgetl(file)
comm_3 = fgetl(file)
A = strsplit(l2,' ');
%Global Temperature
Global_low_temp = str2num(A{2})
Global_mid_temp = str2num(A{3})
Global_high_temp = str2num(A{4})
%Species
for i=1:53
species= fgetl(file);
species_name = species(1:6)
L_1 = fgetl(file);
L1 = strfind(L_1,'E');
C1 = str2num(L_1(1:(L1(1)+3)));
C2 = str2num(L_1((L1(1)+4):(L1(2)+3)));
C3 = str2num(L_1((L1(2)+4):(L1(3)+3)));
C4 = str2num(L_1((L1(3)+4):(L1(4)+3)));
C5 = str2num(L_1((L1(4)+4):(L1(5)+3)));
L_2 = fgetl(file);
L2 = strfind(L_2,'E');
C6 = str2num(L_2(1:(L2(1)+3)));
C7 = str2num(L_2((L2(1)+4):(L2(2)+3)));
C8 = str2num(L_2((L2(2)+4):(L2(3)+3)));
C9 = str2num(L_2((L2(3)+4):(L2(4)+3)));
C10 = str2num(L_2((L2(4)+4):(L2(5)+3)));
L_3 = fgetl(file);
L3 = strfind(L_3,'E');
C11 = str2num(L_3(1:(L3(1)+3)));
C12 = str2num(L_3((L3(1)+4):(L3(2)+3)));
C13 = str2num(L_3((L3(2)+4):(L3(3)+3)));
C14 = str2num(L_3((L3(3)+4):(L3(4)+3)));
%Creating folder for species
mkdir(['C:Program FilesMATLABR2016abin',species_name]);
%Temperature Range(in K)
Temp = linspace(300,5000,200);
%Universal gas constant(In KJ/KgK)
R = 8.314;
%Calculation of Properties
for j = 1:length(Temp)
if Temp(j)<3000
Cp = (C1+C2.*Temp+C3.*Temp.^2+C4.*Temp.^3+C5.*Temp.^4).*R;
H = (C1+(C2.*Temp)./2+(C3.*Temp.^2)/3+(C4.*Temp.^3)/4+(C5.*Temp.^4/5)+C6./Temp).*Temp.*R;
S = (C1*log(Temp)+C2.*Temp+(C3.*Temp.^2)/2+(C4.*Temp.^3)/3+(C5.*Temp.^4)/4+C7).*R;
else
Cp = (C8+C9.*Temp+C10.*Temp.^2+C11.*Temp.^3+C12.*Temp.^4).*R;
H = (C8+(C9.*Temp)/2+(C10.*Temp.^2)/3+(C11.*Temp.^3)/4+(C12.*Temp.^4/5)+C13./Temp).*Temp.*R;
S = (C8.*log(Temp)+C9.*Temp+(C10.*Temp.^2)/2+(C11.*Temp.^3)/3+(C12.*Temp.^4)/4+C14).*R;
end
end
%plotting
%Temperature vs Entropy
figure(1)
plot(Temp,S,'linewidth',3,'color','b')
title(species_name)
xlabel('Temperature')
ylabel('Entropy')
%Temperature vs Enthalpy
figure(2)
plot(Temp,H,'linewidth',3,'color','k')
title(species_name)
xlabel('Temperature')
ylabel('Enthalpy')
%Temperature vs Specific heat
figure(3)
plot(Temp,Cp,'linewidth',3,'color','y')
title(species_name)
xlabel('Temperature')
ylabel('Specific heat')
directory = (['C:Program FilesMATLABR2016abin',species_name]);
cd(directory)
%Saving the File
saveas(1,'Entropy.jpg');
saveas(2,'Enthalpy.jpg');
saveas(3,'Specific heat.jpg');
cd('C:Program FilesMATLABR2016abin')
MM = molecular_mass(species_name)
end
fclose(file)
Result:
Comment Line:
Molecular Weight Of Different Species:
Sample Plots:
C
Enthalpy vs Temperature
Entropy vs Temperature
Specific Heat vs Temperature:
Enthalpy vs Temperature:
Entropy vs Temperature
Specific Heat vs Temperature
Errors: