Project 1 - Parsing NASA thermodynamic data
AIM: To parse a thermodynamic file by NASA.
OBJECTIVE: (.)To exctract the coefficients from the file for each compound.
(.)To calculate molecular weight of each species.
(.)Plot the specific heat(c_p) ,enthalpy(H) ,entropy(S) of each species with respect to both the low and high temperature coeffiecients.
THEORY:
FILE PARSING: File parsing refers to examine a file. Using the parsing,data can be exctrated fromfile which can be stored and can be used to perform calculations. Here's a thermodynamic file generated by NASA is parsed, from which the high temperature and low temperature of various species are exctrated and further stored to calculate the specific heat, enthalpy and entropy.
a)Specific Heat Capacity (c_p):Specific heat capacity is defined as the heat require dto raise the temperature to raise of a given mass by a unit temperature. It is denoted as Cp .It's unit is J/K.
b)Enthalpy(H): Enthalpy refers to internal energy of a system which includes the temperature and pressure. In an enclosed system ,the change in heat is refered to as Enthalpy of a system.It is denoted by H and it's unit is j/mol.
c)Entropy(S): Entropy is a functin of state of system.It is an external property and is reversible.Entropy plays an important role in thermodynamics.Th second law of thermodyanmics is based on entropy which says that entropy of an isolated system never decreases.It is denoted as S and it's J/K.
Description of program:
For all the compound -
1)The file named "THERMO.dat" is given the reading access using 'fopen' command.
2)Next line is clled using the 'fgetl' command and the string is split using the 'strsplit' command.
3)The numbers in the string are converted using 'str2double' command.
4)The global temperatures are extracted using indexing.
5)Similarly the process is repeated in each of the first species and the remainfg coefficients and the local temperatures are extracted.
6)A function is created containing the formulas to calculate thee c_p ,H and ,S and is called in the main program.
7)A range of temperature is created using 'linspace' command.
8)The c_p ,H, and S is calculated for this range of temperature and graphs are plotted.
9)A different directory ias created using the 'mkdir' command to make folders with the name of the species anfd the plots are saved in the particular species name.
10)A function is created to calculate the molecular weight of the species and is called in the main program.
11) 'while ~feof' command is used to run run the file till the end.
12)'if' and 'strcmp' command is used to end the program after string is read as 'END'.
For user defined compound:
1)The file named "THERMO.dat" is given the reading access using the 'fopen' command.
2)Next line is called using the 'fgetl' command and the string is split using the 'strsplit' command.
3)The numbers in the string are converted using the 'str2double' command.
4)The global temperatures are extracted using indexing.
5)The user defined compound is asked using the 'input' command.
6)An 'if' and 'strcmp' conditional statement is used to compare the species name with the user defined input. If it matches, the program runs and generates the desired results else the loop repeats itself until the input matches with the species name to give the desired results.
Function1- Calculate c_p,H and S ;
1)The outputs and inputs having the high temperature coefficients and the low temperature coefficients is determined.
2)An 'if' conditional loop is used to give the condition to use low temperature coefficients or high temperature coeeficients if the temperature is less than mid local temperature and high local temperature respectively.
Fuction2- Calculate the molecular_weight;
1)The common elements and their respective atomic weights are mentioned.
2)A nested for loop is created whee one checks the species name and other checks the position of the element and saves it.
3)An 'if' conditional loop is used in case the molecues are more than one.
4)The molecular weight is calculated accordingly.
5)'sprintf' command is used to print the molecular weight.
MAIN CODE:
clear all
close all
clc
%OPEN THE FILE
f1 = fopen('THERMO.dat','r')
fgetl(f1)
%global temperature
t_line = fgetl(f1);
%SPLITS THE STRING
I = strsplit(t_line,' ');
%CONVERT STRING TO NUMBER
global_low_temperature = str2double(I{2});
global_mid_temperature = str2double(I{3});
global_high_temperature = str2double(I{4});
%NEGLECTING THE 3 LINES EXCLAMATORY LINES
fgetl(f1);
fgetl(f1);
fgetl(f1);
%GET COEFFICIENTS FOR EACH SPECIES
%READING SPECIES BLOCK
%LINE1
for i = 1:53
t_line1 = fgetl(f1);
S = strsplit(t_line1,' ');
species_name = S{1};
A = strfind(t_line1,'G');
temp_values = t_line1(A+4:length(t_line1));
B = strsplit(temp_values,' ');
local_low_temp = str2double(B{1});
local_mid_temp = str2double(B{3});
local_high_temp = str2double(B{2});
%GET COEFFICIENTS FOR EACH SPECIES
t_line = fgetl(f1);
b = strfind(t_line,'E');
%1st coefficient
b1 = t_line(1:b(1)+3);
b1 = str2num(b1);
%2nd coefficient
b2 = t_line(b(1)+4:b(2)+3);
b2 = str2num(b2);
%3rd coefficient
b3 = t_line(b(2)+4:b(3)+3);
b3 = str2num(b3);
%4rd coefficient
b4 = t_line(b(3)+4:b(4)+3);
b4 = str2num(b4);
%5rd coefficient
b5 = t_line(b(4)+4:b(5)+3);
b5 = str2num(b5);
%LINE2
t_line2 = fgetl(f1);
b = strfind(t_line2,'E');
%6th coefficient
b6 = t_line2(1:b(1)+3);
b6 = str2num(b6);
%7th coefficient
b7 = t_line2(b(1)+4:b(2)+3);
b7 = str2num(b7);
%8th coefficient
b8 = t_line2(b(2)+4:b(3)+3);
b8 = str2num(b8);
%9th coefficient
b9 = t_line2(b(3)+4:b(4)+3);
b9 = str2num(b9);
%10th coefficient
b10 = t_line2(b(4)+4:b(5)+3);
b10 = str2num(b10);
%LINE3
t_line3 = fgetl(f1);
b = strfind(t_line3,'E');
%11th coefficient
b11 = t_line3(1:b(1)+3);
b11 = str2num(b11);
%12th coefficient
b12 = t_line3(b(1)+4:b(2)+3);
b12 = str2num(b12);
%13th coefficient
b13 = t_line3(b(2)+4:b(3)+3);
b13 = str2num(b13);
%14th coefficient
b14= t_line3(b(3)+4:b(4)+3);
b14 = str2num(b14);
%universal gas constant
R = 8.314;
t = linspace(local_low_temp,local_high_temp,100);
%calculate
%specific heat by creating function
c_p= specific_heat(b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,local_mid_temp,t,R);
%Enthalpy by creating function
H=enthalpy(b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,local_mid_temp,t,R);
%Entropy by creating function
S = Entropy(b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,local_mid_temp,t,R);
%creating a folder
%plotting for
%temperature vs specific heat
%mkdir([C:\New folder\'species'])
%cd([C:\New folder\'species'])
figure(1)
plot(t,c_p)
xlabel('temperature')
ylabel('specific heat')
title('temperature vs specific heat' , species_name)
pause(0.5)
% image = getframe(gcf);
%imwrite(image.cdata,'t vs cp.png')
saveas(1,'specific heat.png')
%temperature vs enthalpy
figure(2)
plot(t,H)
xlabel('temperature')
ylabel('enthalpy')
title('temperature vs enthalpy' , species_name)
pause(0.5)
%image = getframe(gcf);
%imwrite(image.cdata,'t vs H.png')
saveas(2,'enthalpy.png')
%temperature vs entropy
figure(3)
plot(t,S)
xlabel('temperature')
ylabel('entropy')
title('temperature vs entropy' , species_name)
pause(0.5)
% image = getframe(gcf);
% imwrite(image.cdata,'t vs S.png')
saveas(3,'entropy.png')
%calculating molecular mass
M = mol_wt(species_name);
k = sprintf('%s mol_wt = %f',species_name,M);
disp(k)
end
Function code to calculate c_p :
function[c_p] = specific_heat(b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,local_mid_temp,t,R);
if (t>local_mid_temp)
c_p =(b1+(b2*t)+ (b3*(t.^2))+(b4*(t.^3))+ (b5*(t.^4)))*R
else
c_p =(b8+(b9*t)+ (b10*(t.^2))+(b11*(t.^3))+(b12*(t.^4)))*R;
end
end
Function code to calculate enthalpy:
function[H] = enthalpy(b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,local_mid_temp,t,R)
if (t<local_mid_temp)
H =(b8+((b9.*t)./2)+((b10.*(t.^2))./3)+((b11.*(t.^3))./4)+((b12.*(t.^4))./5)+(b13./t)).* (R.*t);
else
H =(b1+((b2.*t)./2)+((b3.*(t.^2))./3)+((b4.*(t.^3))./4)+((b5.*(t.^4))./5)+(b6./t)).* (R.*t);
end
end
Function code to calculate Entropy:
function[S] = Entropy(b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,local_mid_temp,t,R);
if (t>local_mid_temp)
S= (b1*log(t))+(b2*t)+((b3*(t.^2))/2)+b4*(t.^3/3)+(b5*(t.^4/4))+(b7)*R;
else
S = (b8*log(t))+(b9*t)+((b10*(t.^2))/2)+((b11*(t.^3)/3)+(b12*(t.^4))/4)+b14*R;
end
end
Function code to calculate molecular weight:
function [M] = mol_wt(species_name)
atomic_mass = [15.999,1.008,12.011,14.007,39.948];
elements = ['O','H','C','N','A'];
mol_wt= 0 ;
for i = 1:length(species_name)
for j = 1:length(atomic_mass)
if strcmpi(species_name(i),elements(j))
mol_wt = mol_wt + atomic_mass(j);
z=j;
end
end
t = str2double(species_name(i));
if (t>1)
mol_wt=mol_wt+(atomic_mass(z)*(t-1));
end
end
M = mol_wt ;
end
OUTPUT:
c_p of O2:
Enthalpy of O2: 
Entropy of O2: 
c_p of co2 :
Enthalpy of CO2: 
Entropy of CO2: 
C_p of N2: 
Enthalpy of N2:
Entropy of N2:
Molecular weight of species: 
WORKSPACE: 
RESULTS AND CONCLUSION:
(.)The coefficients were extracted from the file.
(.)The molecular weight were calculated for all the compound .
(.)The graphs were plotted and were saved in their respective compound folder.