Project 1 - Parsing NASA thermodynamic data
The codes :
1) Function code for entropy :
%Funtion for calculating entropy
function [S] = entropy(c1,c2,c3,c4,c5,c7,c8,c9,c10,c11,c12,c14,temp,glob_mid_temp,r)
% providing temperaure condition
% 1-7 are high temp and 8-14 are low temp co-efficient
if(temp<=glob_mid_temp)
S = r.*(c8.*log(temp) +(c9.*temp) +(c10*((temp).^2)/2) + (c11*((temp).^3)/3) + (c12*((temp).^4)/4) + (c14));
else
S = r.*(c1.*log(temp) +(c2.*temp) +(c3*((temp).^2)/2) + (c4*((temp).^3)/3) + (c5*((temp).^4)/4) + (c7));
end
2) Function code for enthalpy :
%Funtion for calculating enthalpy
function [H] = enthalpy(c1,c2,c3,c4,c5,c6,c8,c9,c10,c11,c12,c13,temp,glob_mid_temp,r)
% providing temperaure condition
% 1-7 are high temp and 8-14 are low temp co-efficient
if(temp<=glob_mid_temp)
H = r*temp.*(c8 + ((c9*temp)/2) + ((c10*(temp).^2)/3) + ((c11*(temp).^3)/4) + ((c12*(temp).^4)/5) + ((c13./temp)));
else
H = r*temp.*(c1 + ((c2*temp)/2) + ((c3*(temp).^2)/3) + ((c4*(temp).^3)/4) + ((c5*(temp).^4)/5) + ((c6./temp)));
end
3) Function code for specfic heat :
%Funtion for calculating Specific Heat
function [Cp] = Specific_heat(c1,c2,c3,c4,c5,c8,c9,c10,c11,c12,temp,glob_mid_temp,r)
% providing temperaure condition
% 1-7 are high temp and 8-14 are low temp co-efficient
if(temp<=glob_mid_temp)
Cp = r.*(c8 +(c9.*temp) +(c10*(temp).^2) + (c11*(temp).^3) + (c12*(temp).^4));
else
Cp = r.*(c1 +(c2.*temp) +(c3*(temp).^2) + (c4*(temp).^3) + (c5*(temp).^4));
end
4) Function code for molecular weight :
%function for calculating molecular mass
function W = Molecular_weight(species)
% Atomic name of the species
atomic_name = ['H','C','O','N','Ar'];
% Atomic masses of the species
atomic_weight = [1.008 12.011 15.999 14.007 39.948];
% Molecular weight starting range
W=0;
% loop wrt requirement
for i=1:length(species)
for j=1:length(atomic_name)
% comparing the string value
if strcmp(species(i), atomic_name(j))
W = W + atomic_weight(j);
position = j;
end
end
% converting the srting to number
n = str2num(species);
if n>1
% molecular mass calculation
W = W + atomic_weight(position)*(n-1);
end
end
% printing the calculated values
fprintf('Molecular Weight of %s :- ',species);
fprintf('%f',W);
disp(' ')
end
5) Parsing code for NASA thermodynamic data :
% parsing NASA thermodynamic data
clc
clear all
close all
% input
% universal gas constant
R = 8.314;
% reading from the nasa data file
f1 = fopen('THERMO.dat','r');
% reading the first title line from the file line 1
line1 = fgetl(f1);
% reading the global temperature from the file line 2
temp = fgetl(f1);
A = strsplit(temp,' ');
% converting string file in to numbers
glob_low_temp = str2num(A{2});
glob_mid_temp = str2num(A{3});
glob_high_temp = str2num(A{4});
% Temperature range
T = linspace(glob_low_temp,glob_high_temp,1000);
% skipping comments line (3-5)
for B = 1:3
C = fgetl(f1);
end
% loop for all 53 species
% reading the species line 6
for i = 1:53
tline = fgetl(f1);
S = strsplit(tline,' ');
Species = S{1};
% finding coefficients
% Finding the coefficients from line 7
line2 = fgetl(f1);
x = findstr(line2,'E');
c1 = line2(1:x(1)+3);
c1 = str2num(c1);
c2 = line2(x(1)+4:x(2)+3);
c2 = str2num(c2);
c3 = line2(x(2)+4:x(3)+3);
c3 = str2num(c3);
c4 = line2(x(3)+4:x(4)+3);
c4 = str2num(c4);
c5 = line2(x(4)+4:x(5)+3);
c5 = str2num(c5);
% Finding the coefficients from line 8
line3 = fgetl(f1);
x = findstr(line3,'E');
c6 = line3(1:x(1)+3);
c6 = str2num(c6);
c7 = line3(x(1)+4:x(2)+3);
c7 = str2num(c7);
c8 = line3(x(2)+4:x(3)+3);
c8 = str2num(c8);
c9 = line3(x(3)+4:x(4)+3);
c9 = str2num(c9);
c10 = line3(x(4)+4:x(5)+3);
c10 = str2num(c10);
% Finding the coefficients from line 9
line4 = fgetl(f1);
x = findstr(line4,'E');
c11 = line4(1:x(1)+3);
c11 = str2num(c11);
c12 = line4(x(1)+4:x(2)+3);
c12 = str2num(c12);
c13 = line4(x(2)+4:x(3)+3);
c13 = str2num(c13);
c14 = line4(x(3)+4:x(4)+3);
c14 = str2num(c14);
% Calculating Entropy
S = entropy(c1,c2,c3,c4,c5,c7,c8,c9,c10,c11,c12,c14,T,glob_mid_temp,R);
% Calculating Specific Heat
Cp = Specific_heat(c1,c2,c3,c4,c5,c8,c9,c10,c11,c12,T,glob_mid_temp,R);
% Calculating Enthlpy
H = enthalpy(c1,c2,c3,c4,c5,c6,c8,c9,c10,c11,c12,c13,T,glob_mid_temp,R);
% Calculating Molecular Weight
Mole_wt(i) = Molecular_weight(Species);
% writing molecular wieght in a file
Mole_weight = fopen('Molecular Weight.txt','w');
fprintf(Mole_weight,'Molecular Weight of the species %s is %f ',Species,Mole_wt);
fclose(Mole_weight);
% Plotting specific-heat,enthalpy,entropy with tempreture
% saving in a folder
directory_curr = pwd;
mkdir(Species);
cd(Species);
% Plotting
% plot 1 Enthalphy and Temperaure
figure(1)
plot(T,H,'linewidth',3,'color','r');
xlabel('Temperature [K]');
ylabel('Enthalphy [KJ/mol]');
grid on
title(sprintf('Enthalphy vs Temperature %s',Species));
saveas(figure(1),'Enthalhy.jpg');
% plot 2 Entropy and Temperature
figure(2)
plot(T,S,'linewidth',3,'color','g');
xlabel('Temperature [K]');
ylabel('Entropy [KJ/mol-k]');
grid on
title(sprintf('Entropy vs Temperature %s',Species));
saveas(figure(2),'Entropy.jpg');
% plot 3 Specific Heat and Temperature
figure(3)
plot(T,Cp,'linewidth',3,'color','b');
xlabel('Temperature [K]');
ylabel('Specific Heat [KJ]');
grid on
title(sprintf('Specfic Heat vs Temperature %s',Species));
saveas(figure(3),'Specific Heat.jpg');
% changing the directory
cd(directory_curr);
end
Out put & plots :
Molecular weight of each species :


O2 plots :



N2 plots :



CO2 plots :


Screenshot :

Report :
what is parsing ?
Parsing , syntax analysis or syntactic analysis is the process of analyzing a string of symbols, either in natural languages or computer languages or data structures, conforming to the rules of a formal grammar . The term parsing comes from Latin pars (orationis), meaning part(of speech)
what is entropy ?
Entropy is the loss of energy available to do work . Another form of the second law of thermodynamics states that the total entropy of a system either increases or remains constant; it never decreases. Entropy is zero in a reversible process; it increases in an irreversible process.
what is enthalpy ?
Enthalpy , the sum of the internal energy and the product of the pressure and volume of a thermodynamic system. Enthalpy is an energy-like property or state function—it has the dimensions of energy (and is thus measured in units of joules or ergs), and its value is determined entirely by the tempreture, pressure and composition of the system and not by its history. In symbols, the enthalpy, H, equals the sum of the internal energy, E, and the product of the pressure, P, and volume, V, of the system: H = E + PV.
what is specfic heat ?
The specific heat is the amount of heat per unit mass required to raise the temperature by one degree Celsius.
code explanation :
1) Function code for entropy :
- I have created a function code , that can take inputs of element co-efficients and will provide the value of entropy.
- I have used the provided formula inside the code for calculation of entropy.
- i have also used 'if' condition to differentiate between high and low temp coefficients.
2) Function code for enthalpy :
- I have created a function code , that can take inputs of element co-efficients and will provide the value of enthalpy.
- I have used the provided formula inside the code for calculation of enthalpy.
- i have also used 'if' condition to differentiate between high and low temp coefficients.
3) Function code for specfic heat :
- I have created a function code , that can take inputs of element co-efficients and will provide the value of specific heat.
- I have used the provided formula inside the code for calculation of specific heat .
- i have also used 'if' condition to differentiate between high and low temp coefficients.
4) Function code for molecular weight :
- I have created a Molecular_weight function .
- Then define the main element of all 53 species in an array.
- Then created an array for atomic weight for the main elements.
- Then i assigned molecular starting range W=0 and then created an for loop condition which runs to the length of the species
and an another nested for loop is created which runs to the length of the string atomic_name.
- Then i created an condition using 'if' command and used 'strcmp' command to compare the species and atomic_name strings to calculate molecular masses if by comparison both string found same then we will assign its molecular weight.
- Then i string to the number using 'str2num' command assign to 'n'. Then i ran a condition of if loop to find weather the species contains more than one element if there are more then 1 elements that is 'n>1' then we will increment the atomic weight by the multiplication of position and 'n- 1' to the atomic_weight array.
- Then i printed the Molecular weight using 'fprintf' command and displayed it using 'disp' command.
5) Function code for Parsing NASA thermodynamic data :
- started the program with clear all , close all , clc.
- provided the code the value for 'r' as input .
- with the command 'fopen' i opened the nasa thermodynamic file for the code.
- used 'fgetl' in the code to skip comment lines.
- to read the global tempreture line i have used the 'strsplit' command.
- to define the tempreture i identified by position of spaces between them and then converted the string to number with the command 'str2num'.
- created an array for tempreture range using 'linspace' command.
- skipped the 3 comment lines at a time using a for loop with 'fgetl' command.
- all the species co-efficients are arrange in a similar fashion , so i used for loop for all 53 species .
- again to read the species name i have used 'strsplit' command.
- and to read the co-efficients , i have used 'findstr' command , it identifies the position of 'E' in the co-efficient.
- by using ':' and taking 'E' as reference i have identified the co-efficients.
- Then with the command 'str2num' i converted the co-efficients from string to number.
- then by calling the functions and providing them the co-efficients values , i have calculated "Cp, H , S and molecular weight"
- Then i created a file where i wrote all the molecular weight wrt its species and saved it in a folder, using 'fopen' , 'fprint' command.
- then i created plots for cp vs temp , H vs temp, s vs temp for every species. and stored the 3 plots for each species in a separate folder.
References :
- https://in.mathworks.com/matlabcentral/answers/196582-best-way-to-parse-text-file
- https://youtu.be/t0oNbYJn2Mw
- https://in.mathworks.com/help/matlab/matlab_prog/parse-function-inputs.html