Project 1 - Parsing NASA thermodynamic data
PROJECT- Parsing NASA thermodynamic data
AIM:
- To develop a function that extracts the 14 coefficients and calculates the enthalpy, entropy, and specific heats for all the species in the data file.
- Develop a code to calculate the molecular weight of each species and display it in the command window.
-
To plot the Cp, Enthalpy, and Entropy for the local temperature range (low temperature: high temperature) specific for each species.
-
To save the plots as images with appropriate names and dump them in separate folders for each species with a suitable name. All of these should be generated automatically
PRINCIPLE:
NASA came up with polynomials that can be used to evaluate thermodynamic properties such as Cp, H and S. They have also documented the co-efficients that are required to evaluate these polynomials for 1000+ species.
OBSERVATIONS:
Steps for choosing the logic of code and file parsing -
Step 1: - We open the file and parse the file ,fopen is used to open a file.
Step 2: - To navigate throughout the file and using (fgetl) command to get the line of the file and read the file line by line to get to the desired result.
Step 3: - Reading the text from the header file and read the file line by line
Step 4: - String split command then cell array is generated. A cell array is a special data structure to store data of the different type and perform the desired task
Step 5: - The function for Cp, H, S is created to calculate them by taking the values from the file data
Step 6: - Molecular weight is calculated using the molecular weight function and is printed on the command window
Step 7: - The plots for Cp, H, S are created for each species and saved in a folder in jpg format to access the species with different images
Step 8: - Creating folders for different molecules and save the plots for specific heat, enthalpy, and entropy in that folder and display the molecular weight of different species in the command window
Here R is the universal gas constant, T is the temeprature
We will use these above the Specific Heat, Entropy, Enthalpy as function code in Matlab and used to calculate the above and plot the graph accordingly.
File parsing is a beneficial technique. Parsing a file 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 and It means, to a computer, about what “reading text” means to a human being. You scan it, from start to finish, and figure out what it means as you go - whether you’re” a person reading text or a computer parsing a file.
File Parsing is how a piece of file information can be used in a computer program. Read data from external files and databases and include that data into your calculation. File Writing is writing data in a particular file.
CALCULATIONS:
FUNCTION CODES for all funcitions used-
1.Enthalpy-
is a property of a thermodynamic system, defined as the sum of the system's internal energy and the product of its pressure and volume.It is a convenient state function standardly used in many measurements in chemical, biological, and physical systems at a constant pressure.
Function Code:
%Funtion for the enthalpy
function [H] = enthalpy_H(x1,x2,x3,x4,x5,x6,x8,x9,x10,x11,x12,x13,temp,gb_m,r)
%Checking for the temperaure
%The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients
if(temp<=gb_m)
H = r*temp.*(x8 + ((x9*temp)/2) + ((x10*(temp).^2)/3) + ((x11*(temp).^3)/4) + ((x12*(temp).^4)/5) + ((x13./temp)));
else
H = r*temp.*(x1 + ((x2*temp)/2) + ((x3*(temp).^2)/3) + ((x4*(temp).^3)/4) + ((x5*(temp).^4)/5) + ((x6./temp)));
end
2.ENTROPY-
Entropy is a scientific concept, as well as a measurable physical property that is most commonly associated with a state of disorder, randomness, or uncertainty.
Function Code-
%Funtion for the entropy
function [S] = entropy_S(x1,x2,x3,x4,x5,x7,x8,x9,x10,x11,x12,x14,temp,gb_m,r)
%Checking for the temperaure
%The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients
if(temp<=gb_m)
S = r.*(x8.*log(temp) +(x9.*temp) +(x10*((temp).^2)/2) + (x11*((temp).^3)/3) + (x12*((temp).^4)/4) + (x14));
else
S = r.*(x1.*log(temp) +(x2.*temp) +(x3*((temp).^2)/2) + (x4*((temp).^3)/3) + (x5*((temp).^4)/4) + (x7));
end
3.SPECIFIC HEAT-
In thermodynamics, the specific heat capacity (Cp) of a substance is the heat capacity of a sample of the substance divided by the mass of the sample.
Function Code-
%Funtion for the Specific Heat
function [Cp] = Specific_heat(x1,x2,x3,x4,x5,x8,x9,x10,x11,x12,temp,gb_m,r)
%Checking for the temperaure
%The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients
if(temp<=gb_m)
Cp = r.*(x8 +(x9.*temp) +(x10*(temp).^2) + (x11*(temp).^3) + (x12*(temp).^4));
else
Cp = r.*(x1 +(x2.*temp) +(x3*(temp).^2) + (x4*(temp).^3) + (x5*(temp).^4));
end
4.MOLECULAR WEIGHT:
The molecular mass is the mass of a given molecule: it is measured in daltons. Different molecules of the same compound may have different molecular masses because they contain different isotopes of an element.
Fucntion Code-
%function calculate molecular mass
function mw = mol_wt(sp_name)
%storing value
compound_name = (sp_name)
% creating an array of elements
key_at = ['C','H','O','N','A'];
%storing the atomic mss value in the array
value_at = [12.0107 1.00794 15.994 14.0067 39.948];
%asssigning molecular mass as 0
mw =0 ;
%creating for loop to perform the task
for i = 1:length(compound_name)
for j = 1:length(key_at)
%comparing the string value
if strcmp(compound_name(i),key_at(j))
%if above comparision is true value of molecular mass is
%calculated
mw =mw + value_at(j);
%storing the position for further use
pos = j;
end
end
%converting the srting to number
k = str2num(compound_name(i));
%comparing if the value of k is greater than executed the further statements
if(k>1)
%molecular mass calculation
mw = mw + value_at(pos)*(k-1);
end
end
%command to print the molecular masss of the molecule
fprintf('Molecular Weight of the species %s is %f',compound_name ,mw)
end
MAIN CODE for NASA file parsing and plot generation-
%File Parsing for a file
clc
clear all
close all
f1 = fopen('THERMO.dat','r');
first_line = fgetl(f1);
%R is the universal gas constant J/(mol-K)
R = 8.314;
temp = fgetl(f1);
%Reading text from a header file
%Reading the global temperature from the next line
A = strsplit(temp,' ');
%Cell array is generated 1x4 cell array
glob_lowtemp = str2num(A{2});
glob_midtemp = str2num(A{3});
glob_hightemp =str2num(A{4});
%Temperature range
Temperature = linspace(glob_lowtemp,glob_hightemp,1000);
%skipping the three comment lines
for j =1:3;
k =fgetl(f1);
end
%running for loop fopr 53 times as there are 53 species
for i = 1:53;
%Parsing of the species block
tline = fgetl(f1);
S =strsplit(tline,' ');
Species_name =S{1};
%Extracting the coefficients from first line
line2 =fgetl(f1);
x =findstr(line2,'E');
%Finding the 5 coefficient
x1 = line2(1:x(1)+3);
x1 = str2num(x1);
x2 = line2(x(1)+4:x(2)+3);
x2 = str2num(x2);
x3 = line2(x(2)+4:x(3)+3);
x3 = str2num(x3);
x4 = line2(x(3)+4:x(4)+3);
x4 = str2num(x4);
x5 = line2(x(4)+4:x(5)+3);
x5 = str2num(x5);
%line3 5coefficients
line3 = fgetl(f1);
x = findstr(line3,'E');
x6 = line3(1:x(1)+3);
x6 = str2num(x6);
x7 = line3(x(1)+4:x(2)+3);
x7 = str2num(x7);
x8 = line3(x(2)+4:x(3)+3);
x8 = str2num(x8);
x9 = line3(x(3)+4:x(4)+3);
x9 = str2num(x9);
x10 = line3(x(4)+4:x(5)+3);
x10 = str2num(x10);
%Line4 4coefficients
line4=fgetl(f1);
x = findstr(line4,'E');
x11 = line4(1:x(1)+3);
x11 = str2num(x11);
x12 = line4(x(1)+4:x(2)+3);
x12 = str2num(x12);
x13 = line4(x(2)+4:x(3)+3);
x13 = str2num(x13);
x14 = line4(x(3)+4:x(4)+3);
x14 = str2num(x14);
% Entropy Calculation
S = entropy_S(x1,x2,x3,x4,x5,x7,x8,x9,x10,x11,x12,x14,Temperature,glob_midtemp,R);
%Cp calculation means Specific Heat calculation
Cp = Specific_heat(x1,x2,x3,x4,x5,x8,x9,x10,x11,x12,Temperature,glob_midtemp,R);
%Enthlpy Calculation is H
H = enthalpy_H(x1,x2,x3,x4,x5,x6,x8,x9,x10,x11,x12,x13,Temperature,glob_midtemp,R);
%Molecular Weight calculation of different elements
mw(i) = mol_wt(Species_name);
%Reading and writinh of the molecular wieght in the list of molecular
%wieght
Moll_wt = fopen('Molecular Weight.txt','w');
fprintf(Moll_wt,'Molecular Weight of the species %s is %f ',Species_name,mw);
fprintf(Moll_wt,'/n');
fclose(Moll_wt);
% Plotting on the graph for species fo the temperature and specific
% heat
%Creating a folder for saving the species with its properties
directory_curr = pwd;
mkdir(Species_name);
cd(Species_name);
%Plotting the graph in different plotting window
%plot 1 for the temperature and entropy
%plot 2 for the temperature and enthalpy
%plot 3 for the temperature and specific heat
%plot 1 Enthalphy and Temperaure
figure(1)
plot(Temperature,H,'linewidth',3,'color','b');
xlabel('Temperature in [K]');
ylabel('Enthalphy [KJ/mol]');
grid on
title(sprintf('Variation in Enthalphy wrt Temperature %s',Species_name));
saveas (figure(1),'Enthalhy.jpg');
%plot 2 Entropy and Temperature
figure(2)
plot(Temperature,S,'linewidth',3,'color','r');
xlabel('Temperature in [K]');
ylabel('Entropy [KJ/mol-k]');
grid on
title(sprintf('Variation in Entropy wrt Temperature %s',Species_name));
saveas (figure(2),'Entropy.jpg');
%plot 3 Specific Heat and Temperature
figure(3)
plot(Temperature,Cp,'linewidth',3,'color','g');
xlabel('Temperature in [K]');
ylabel('Specific Heat [KJ]');
grid on
title(sprintf('Variation in Specific Heat wrt Temperature %s',Species_name));
saveas (figure(3),'Specific Heat.jpg');
cd(directory_curr);
end
Code breakdown-
Starts with clear close clc commands.
uses command to open a file in read mode for the thermo.dat file and to read file line by line.
Defines universal gas constant.
saves the line in the temp variable for further procedures.
command to split the line saved in temp variable when a literal space is found.
cell array generated for assigning the global temperatures.
command to generate the 1000 values between the range of temperature.
for loop to parse the file and create plot for 53 molecules in the file.
extracting each coefficients from the line.
Entropy Calculation be above found values and using an entropy function
Enthalpy calculated using an function
Specific heat is found using function created
Molecular weight is calculated for 53 molecules and displayed in command window.
Creating a folder for saving the species with its properties
plot 1 Enthalphy and Temperaure
plot 2 Entropy and Temperaure
plot 3 Specific Heat and Temperaure
labels the axes
grid on command for more details
title command to name plots
saveas command to save the plot as jpg file in the folder made.
OUTPUT:
Screenshot of the window where all the folders are saved-

Plots for o2-



Plots for N2-



Plots for CO2-



MOLECULAR WEIGHT of the Molecules-






CONCLUSION:
Hence parsing of the file was done with plot generation. And plots for 53 molecules were generated,and molecular weight of each 53 molecules has been calculated. All datas created were saved in a folder.