Project 1 - Parsing NASA thermodynamic data
AIM:-
calculates the enthalpy, entropy and specific heats for all the species in the data file.
To display T vs Cp, T vs Enthalpy, T vs Entropy plots and display molecular weight for the required species
OBJECTIVE:-
- Write a function that extracts the 14 coefficients and calculates the enthalpy,entropy and specific heats for all the species in the data file.
- Calculating molecular weight of each species.
- Plotting the specific heat,enthalpy and entropy for the local temperature range.
- Saving the plots as images with appropriate names and dumping them in seperate folders for each species.
FILE PARSING:-
Parsing in computer languages refers to syntactic analysis of the input data normally from a file 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 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.
PARSER:-
A parser is a software component that takes input data (frequently text) and builds a data structure often some kind of parse tree, abstract syntax tree or other hierarchical structure, giving a structural representation of the input while checking for correct syntax. The parsing may be preceded or followed by other steps, or these may be combined into a single step.
MOLECULAR WEIGHT:-
Molecular weight is a measure of the sum of the atomic weight values of the atoms in a molecule. Molecular weight is used in chemistry to determine stoichiometry in chemical reactions and equations. Molecular weight is commonly abbreviated by M.W. or MW. Molecular weight is either unitless or expressed in terms of atomic mass units (amu) or Daltons (Da).
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. The term and the concept are used in diverse fields.
SPECIFIC HEAT CAPACITY:-
specific heat capacity (symbol cp) of a substance is the heat capacity of a sample of the substance divided by the mass of the sample. Informally, it is the amount of energy that must be added
Enthalpy
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.
Function coding for Formula calculating the Cp, H, S:
In this coding using function command, the formula is developed.
Here temperatures vary from each species where the first 7 will be local high temperature and the rest 7 species will the local low temperature. So here we use if and else command to calculate both temperatures.
Formula:
PROGRAM:-
% Specific heat, Enthalpy & Entropy Calculation
function [Cp,H,S] = inputs(T,R,Tm1,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14)
for i = 1:length(T)
if T(i)>Tm1
Cp(i)= (c1 + c2.*T(i) + c3.*T(i).^2 + c4.*T(i).^3 +c5.*T(i).^4).*R;
H(i) = (c1 + (c2.*T(i))./2 + (c3.*T(i).^2)./3 + (c4.*T(i).^3)./4 +(c5.*T(i).^4)./5 + c6./T(i)).*T(i)*R;
S(i) = (c1.*log(T(i)) + c2.*T(i) + (c3.*T(i).^2)./2 + (c4.*T(i).^3)./3 +(c5.*T(i).^4)./4 + c7).*R;
else
Cp (i)= (c8 + c9.*T(i) + c10.*T(i).^2 + c11.*T(i).^3 +c12.*T(i).^4).*R;
H(i) = (c8 + (c9.*T(i))./2 + (c10.*T(i).^2)./3 + (c11.*T(i).^3)./4 +(c12.*T(i).^4)./5 + c13./T(i)).*T(i)*R;
S (i)= (c8.*log(T(i)) + c9.*T(i) + (c10.*T(i).^2)./2 + (c11.*T(i).^3)./3 +(c12.*T(i).^4)./4 + c14).*R;
end
end
end
Function coding for calculating the Molecular Weight:-
In this molecular weight is calculated using function command here atomic name and atomic weight are given as inputs to the species. A loop is created for calculating molecular weight of species individually,strcmp is used to compare species with the elements and calculate their molecular weight. Then string is converted in to number using str2double command.
Main Code explained:-
-Initially we will opening the given THERMO.dat file using fopen command and then we will extract all the information provided in the file using fgetl command.
-First we we will get the global temperature by using strslplit command and then we skip some lines using the fget command.
-Now we make use of for loop command to get all temperature and coefficients that are presnt in our file.
-For getting temperature and coefficients we use finstr command inorder to input the accurate spot and specifying it so that we can get all the data.
-Now the datas which are obtanied from the file will be of string format which we need to convert into numbers by using str2num command.and by this we will get the temperature and coefficient of all the species.
-Now we make use of if command to create file for each species, in case if the folder exist it will simply save as plot or else it will create a folder to save the plot.
-Now the specific heat,enthalpy ,entropy function is calculated and gets plotted.
-The three figures are plotted using plot command and each species name is displayed using title command.And further it will named using xlabel and ylabel command.
-Now we are changing the dierectory using cd command and then we will save the plots as our images using save command.
-Finally we will again change the dierectory using cd command to calculate molecular weight of each species using the function program and closing the file using fclose command.
clear all
close all
clc
f1= fopen('THERMO.dat','r');
fgetl(f1);
tline = fgetl(f1);
A = strsplit(tline,'.');
Global_low_temp = str2num(A{2});
Global_mid_temp = str2num(A{3});
Global_high_temp = str2num(A{4});
%LINES
for i = 3:5
g =fgetl(f1);
end
%tempeature
for j = 1:53
species =fgetl(f1);
t= strfind(species,'.');
%local tempeature
T1= species(52-3:t(1)+3);
T11 =str2num(T1);
species(t(1)+6:62+3);
Th = species(t(1)+ 6:t(2)+3);
Th1 = str2num(Th);
species(t(2) +6:72+3);
Tm = species(t(2)+6:t(3)+3);
Tm1 = str2num(Tm);
h=strsplit(species,' ')
sp_name = h{1}
%co-efficentent
coeff_line_1 = fgetl(f1);
A11 = strfind(coeff_line_1,'E');
coeff_line_1(1:12+3);
a1 = coeff_line_1(1:A11(1)+3);
c1 = str2num(a1);
a2 = coeff_line_1(A11(1)+4:A11(2)+3);
c2 = str2num(a2);
a3 = coeff_line_1(A11(2)+4:A11(3)+3);
c3 = str2num(a3);
a4 = coeff_line_1(A11(3)+4:A11(4)+3);
c4 = str2num(a4);
a5 = coeff_line_1(A11(4)+4:A11(5)+3);
c5 = str2num(a5);
% coefficent 1
coeff_line_2 = fgetl(f1);
A12 = strfind(coeff_line_2,'E');
coeff_line_2(1:12+3);
a6 = coeff_line_2(1:A12(1)+3);
c6 = str2num(a6);
a7 = coeff_line_2(A12(1)+4:A12(2)+3);
c7 = str2num(a7);
a8 = coeff_line_2(A12(2)+4:A12(4)+3);
c8 = str2num(a8);
a9 = coeff_line_2(A12(3)+4:A12(4)+3);
c9 = str2num(a9);
a10 = coeff_line_2(A12(4)+4:A12(5)+3);
c10 = str2num(a10);
%coefficent 2
coeff_line_3 = fgetl(f1);
A13 = strfind(coeff_line_3,'E');
coeff_line_3(1+13+3);
a11 = coeff_line_3(1:A13(1)+3);
c11 = str2num(a11);
a12 = coeff_line_3(A13(1)+4:A13(2)+3);
c12 = str2num(a12);
a13 = coeff_line_3(A13(2)+4:A13(3)+3);
c13 =str2num(a13);
a14 = coeff_line_3(A13(2)+4:A13(4)+3);
c14 = str2num(a14);
R = 8.314
T =linspace(T11,Th1,100);
% cd(['C:\Users\Student\Desktop\file parsing\'])
[CP,H,S] = inputs(T,R,Tm,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14);
% Plotting
figure(1)
plot(T,CP,'linewidth',2,'color','r')
grid on
title(sp_name)
xlabel('Temperature')
ylabel('Specific Heat')
figure(2)
plot(T,H,'linewidth',2,'color','b')
grid on
title(sp_name)
xlabel('Temperature')
ylabel('Enthalpy')
figure(3)
plot(T,S,'linewidth',2,'color','y')
grid on
title(sp_name)
xlabel('Temperature')
ylabel('Entropy')
% program = (['D:\matlab programs\NASA File parsing\species\',sp_name'])
mkdir(['C:\Users\Student\Desktop\file parsing\plots\',sp_name])
cd(['C:\Users\Student\Desktop\file parsing\plots\',sp_name])
%
%saving the plot
saveas(1,'specific Heat.jpg')
saveas(2,'Enthalpy.jpg')
saveas(3,'Entropy.jpg')
% cd('D:\matlab programs\NASA file parsing')
species = sp_name;
elements = ['H','C','O','N','A','S'];
atomic_weight = [1.007 12.0107 15.999 14.0067 39.948 32.065];
molecular_weight_calculation = 0;
for i = 1:length(species)
for j = 1:length(elements)
if strcmp(species(i),elements(j))
molecular_weight_calculation = molecular_weight_calculation+atomic_weight(j);
a = j;
end
end
n = str2num(species(i));
if n>1;
molecular_weight_calculation = molecular_weight_calculation+atomic_weight(a)*(n-1)
end
end
% cd(['C:\Users\Student\Desktop\file parsing\plots',sp_name])
end
fclose(f1)
ERRORS:-

OUTPUTS:-
Screenshot containing the plots for all the species:-

O2 PLOTS:-



N2 PLOTS:-



CO2 PLOT:-



Molecular weight of species in command window:-

RESULT:-
The code to prase the NASA thermodynamic data file has been programmed and the thermodynamic properties and molecular weight of species has been calculated and plotted.