Project 1 - Parsing NASA thermodynamic data
NASA File Parsing
Aim: To extract the 14 Co-efficient from the NASA data file and perform calculation to find Specific heat (Cp), Enthalpy(H), Entropy(S), molecular weight and to plot the graph for all species in the data.
File parsing:
It is a method which is used to read a data form a file to perform some use full task by classification. The file parsing is a type of computer language that aims to read the data file and provide a meaning to the string or character or to assign them to some variable. The concept of file parsing is mainly used in the process of analysis performed over the data in computers which results in categorized data that which can be imposed to analytical process.
In this we have performed parsing over the NASA thermodynamic data file which contains the thermodynamic properties for each specious like oxygen, hydrogen etc.
The coefficient of each species and their respective local temperature are extracted and they are use to find out the Cp, H, S.
Formula Used: The formula used combines coefficients and their respective local temperature to find the Cp, H, S.
`Cp = (a1 + a2*T + a3*T^2 + a4*T^3 + a5*T^4)*R`
`H = (a1*T + (a2*T^2)/2 + (a3*T^3)/3 + (a4*T^4)/4 + (a5*T^5)/5 + a6)*R`
`S = (a1*log(T) + a2^T + (a3*T^2)/2 + (a4*T^3)/3 + (a5*T^4)/4 + a7 )*R`
Where,
Cp – Specific heat
H - Enthalpy
S – Entropy
T-local temperature
R – Gas constant
a1,a2,a3,…….. – temperature coefficient
Objective of the Project:
- To code a program to extract the 14 co-efficient from the THERMO data file for all the 53 species by skipping all comment lines.
- To perform calculation to find specific heat, enthalpy, entropy by substituting all the coefficient of temperatures to their respective formulas which provides the change of parameters with respect to temperature for each species.
- To find the molecular weight of each species and to display them in the comment window.
- Plot the graph local temperature Vs specific heat, enthalpy, entropy for each element.
- All the plot created must be saved to the in separate folders with the species name containing all the three plots.
Body of content: -
Codes:
NASA's main code
%NASA
clear all
close all
clc
%To open the data file to read
f=fopen('thermo.dat','r');
fgetl(f); %skipping first title line
t_line=fgetl(f); %assigning Global temperature
temp=strsplit(t_line,' '); %splitting global temperature with respect to space
temp=str2double(temp); %converting each seperate str value of temperature to numbers
%assiging each of the Golbal temperatures
low_global_temp=temp(2);
medium_global_temp=temp(3);
high_global_temp=temp(4);
%Skipping the first three line of comment lines
for i = 1:3
comments_to_skip = fgetl(f);
end
%Iterating the codes for all the 53 species
for i= 1: 53
species_ln=fgetl(f); %assigninh first line of species with name and temperatures
species=strsplit(species_ln,' '); %splitting the first line by ommiting the space and storing values in each cell
species_name=species{1}; %converting the species name on index 1 from cell to sting and then assigning
high_temp=str2double(species(end-1)); %assigning temperatures of the specimen to variables
medium_temp=str2double(species(end-2));
low_temp=str2double(species(end-3));
%assigning second line of first specimen to l1
l1=fgetl(f);
r1=strfind(l1,'E'); %obtaining the index value of string 'E' in the first
%using the index of 'E' indudival temperatures are extraced by providing boundary condition
a1=str2double(l1(1 : r1(1)+3));
a2=str2double(l1(r1(1)+4 : r1(2)+3));
a3=str2double(l1(r1(2)+4 : r1(3)+3));
a4=str2double(l1(r1(3)+4 : r1(4)+3));
a5=str2double(l1(r1(4)+4 : r1(5)+3));
%assigning third line of first specimen to l2
%following the same procedure as the first line
l2=fgetl(f);
r2=strfind(l2,'E');
a6=str2double(l2(1 : r2(1)+3));
a7=str2double(l2(r2(1)+4 : r2(2)+3));
a8=str2double(l2(r2(2)+4 : r2(3)+3));
a9=str2double(l2(r2(3)+4 : r2(4)+3));
a10=str2double(l2(r2(4)+4 : r2(5)+3));
%assigning fourth line of first specimen to l3
%following the same procedure as the first line
l3=fgetl(f);
r3=strfind(l3,'E');
a11=str2double(l3(1 : r3(1)+3));
a12=str2double(l3(r3(1)+4 : r3(2)+3));
a13=str2double(l3(r3(2)+4 : r3(3)+3));
a14=str2double(l3(r3(3)+4 : r3(4)+3));
%Creating a new folder by providing a path to save the plots
mkdir(['C:\Users\Hema Varshini\Desktop\matlab\file_parsing\',species_name]);
%creating 500 values for temperature from local min and max temperature
T = linspace(low_temp,high_temp,500);
%calling the function with all the extraced coefficient and local temperatures
[cp ,h ,s ] = coeff(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T);
%figure one for plotting specific heat
figure(1)
%plot between local temperature and specific heat
plot(T,cp,'linewidth',3)
%titeling th eplot wit the variable species name
title(species_name)
%lebeling the axis of the plot
xlabel('Temperature')
ylabel('specific heat')
%figure two for plotting enthalpy
figure(2)
%plot between local temperature and enthalpy
plot(T,h,'linewidth',3)
title(species_name)
xlabel('Temperature')
ylabel('Enthalpy')
%figure three for plotting entropy
figure(3)
%plot between local temperature and entropy
plot(T,s,'linewidth',3)
title(species_name)
xlabel('Temperature')
ylabel('Entropy')
%creating indudival foldor for each specimen with the specimen name
directory=(['C:\Users\Hema Varshini\Desktop\matlab\file_parsing\',species_name]);
cd(directory)
%saving all the three plots of each element in jpg format
saveas(1,'specific heat.jpg')
saveas(2,'Enthalpy.jpg')
saveas(3,'Entropy.jpg')
cd('C:\Users\Hema Varshini\Desktop\matlab\file_parsing')
%printing the name of the specimen in the iteration
%calling the molecular weight function with the spcies name
Molecular_weight= mole_weight(species_name)
end
Function for coefficient
%function to calculate the specfic heat, Enthalpy and Entropy
function [cp ,h ,s ] = coeff(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T)
%variable R is declared with the value of Global gas constant
R=8.314;
%Iterating with all the 500 temperatures
for j = 1:length(T)
%if the temperature is below the the least global temperature
if T(j)<300
%specfic heat
cp = (a1 + a2.*T + a3.*T.^2 + a4.*T.^3 + a5.*T.^4).*R;
%Enthalpy
h = (a1.*T + (a2.*T.^2)./2 + (a3.*T.^3)./3 + (a4.*T.^4)./4 + (a5.*T.^5)./5 + a6).*R;
%Entropy
s = (a1.*log(T) + a2.^T + (a3.*T.^2)./2 + (a4.*T.^3)./3 + (a5.*T.^4)./4 + a7 ).*R;
%if the temerature is above the gobal least temperature
else
cp = (a8 + a9.*T + a10.*T.^2 + a11.*T.^3 + a12.*T.^4).*R;
h = (a8.*T + (a9.*T.^2)./2 + (a10.*T.^3)./3 + (a11.*T.^4)./4 + (a12.*T.^5)./5 + a13).*R;
s = (a8.*log(T) + a9.^T + (a10.*T.^2)./2 + (a11.*T.^3)./3 + (a12.*T.^4)./4 + a14 ).*R;
end
end
end
Function for obtaining molecular weight
%function to calculate molecular weight of each species
function [mol_wt]=mole_weight(species_name)
%covert in the species nmae to upper case
species=upper(species_name)
%declariing the arrya of elements
constituents={'H','C','N','O','A'};
%provising the atomic number for the elements respectively
atomic_wt=[1 12 14 16 40];
%assigning MW is 0
mol_wt=0;
%iterating i from 1 to length of the species name
for i=1:length(species)
%iterating i from 1 to length of the elements
for j=1:length(constituents)
%comparing the species string with the elements
if strcmp(species(i),constituents(j))
%adding the aomic number of the matched element
mol_wt=mol_wt+atomic_wt(j);
u=j;
end
end
%converting the str of species to double
n=str2double(species(i));
%if n of NaN is greater than 1 perform the calculation
if n>1
mol_wt=mol_wt+atomic_wt(u)*(n-1);
end
end
end
Codes explanation: -
NASA main code:
- The code starts with common commands clear all, close all, clc with clears workspace, opened data file, command window.
- Then the NASA’s thermodynamic data file in the path is opened for read by using the fopen command and assigned to variable
- First tittle line is skipped by fgetl and the second line with global temperatures are assigned to variable
- Then the elements in the second line is separated by reference with space and then all the elements are saved to the variable temp as induvial cells.
- Then values in the variable temp are converted from string to numeric values.
- Then each value of the global temp is saved as low, medium and high by providing the index values.
- The next three comment lines are skipped by iterating with fgetl.
- As we know that the total number of specimens are 53, we iterate i from 1 to 53.
- Inside the for loop the first line containing specimen names and local temperature are extracted and provided to variable species_ln, in the next line each elements in the line is separated with reference to space and each cell of values are assigned to variable species, the first element in the variable is taken as
- The local temperature is stored in their respective variables are extracted with the index values of the cell.
- The line with the coefficient of temperatures are spited into section with reference to the value ’E’, and the index values of ‘E’ is stored to
- by having the index value of r1 as reference the coefficient are converted from string to numeric and are assigned to their respective a value and the same process is continued for all the tree lines of the species for the 14 co-efficient of temperature.
- A new folderr by providing a path to save the plots is created by providing the path to create with the inbuild function mkdir.
- The temperature ranging between low local temperature to high local temperature of 500 values are created with the function linspace.
- Then the function coeff is called with all the extracted coefficient and the array of T values.
- The result obtained from the function for Cp, H. S are used to create three different plots with local temperatures Vs (specific heat, Enthalpy, Entropy) with the title name of species.
- All these plots are saved in jpg format to the newly created folders with the species name.
- Then the function to find out the molecular weight is used to obtain the molecular weight of all the species.
Function for coefficient:
- All the values of coefficient and the temperature array are loaded while calling.
- variable R is declared with the value of Global gas constant.
- for loop is created to iterate from 1 to length of T.
- If condition is provided with formula that if the local temperatures are less than 300 than to use coefficient from a1 to
- Else If the temperate is more than 300 than use coefficient from a8 to a14 to the formula.
Function for molecular weight:
- Function is called with the species name.
- The species name given is converted to upper case.
- Constituents and their respective atomic weight are provided with the variables Constituents and
- For loops for length of species and constituents are initialized and the string value with the index value of I and constituents are compared, if they match with each other then their respective atomic weight is added until all the elements of the specimen name is iterated.
- the specimen of i is converted into numeric value and is saved in the variable n and then they are used to calculate the molecular weight to the variable mol_wt.
Plots:
Plot files:
conclusion:
The parsing of NASA’s thermodynamic file is done to calculate all the specific heat, enthalpy, entropy and the molecular weight and the plots are plotted.
Errors:
when i was trying to run the program i frounf that the title for the plot showing error as exceed the index value
then is error was soved by removing the title variable which i used to store the title of the data fiile which got interchanged with the inbuild title variable for plot.