Project 1 - Parsing NASA thermodynamic data
AIM :
- To write a matlab program to extract the 14 -coefficients from NASA thermodynamic data file and to calculates the enthalpy , entropy and specific heats for all the species in the data file .
- Ploting the graph of every species and to find molecular mass of all species .
DESCRIPTION :
- File parsing is done to extract data from the file ,it should be as generic as possible to fetch the data from different files
- In this program we are getting the 14 co-efficients from the data and by using this we calculate enthalpy,entropy and specific heat for different species and the condition is that the FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficient are LOW-temperature coefficients.
- Below is the formula to calculate specific heat , enthalpy and entropy:
Here R is the universal gas constant, T is the temeprature.
Program Algorithm :
- fopen = it is used to open the file name 'THERMO.dat'.
- fgetl = it is used to read line of the file.
- strsplit = string is splited using this command and the global temperatures are fetched.
- Next three line is generally comments.
- There are 53 species and and the logic remains the same hence 'for loop' for the 53 species is declared and the main program is given inside the 'for loop'
- In order to get coefficients first we have find the position of 'E' by using 'strfind' command.Then with the help of this we are able to find the coefficients.
- mkdir command is used to create the seperate folder of of each species.
- linspace command is used to split the temperature .
- To assign high and low temperature co-efficients each and every local temperature is compared with the local mid temperature .
- plot command is used to plot graps of enthalpy , entropy and specific heat .
- To find the molecular mass for each and every element atomic weight is assigned now by using 'strcmp' command each species elements are compared and molecular mass is stored using two 'for loop',one to iterate the species element and one to compare the elements.
- in some species like HNO3 the elements atomic weight to be added twice or thrice , hence flag operator is used to get the position and that element atomic weight is added to the molecular weight by using 'if' condition .
MATLAB PROGRAM :
clear all
close all
clc
% finding and opening the file
f1 = fopen('THERMO.dat')
% heading
line1 = fgetl(f1);
% Global temperature values
line2 = fgetl(f1);
A = strsplit(line2,' ');
global_min_temp = str2num(A{2});
global_mid_temp = str2num(A{3});
global_max_temp = str2num(A{4});
%skipping comments
line3 = fgetl(f1);
line4 = fgetl(f1);
line5 = fgetl(f1);
for i= 1:53
header = fgetl(f1);
x=strsplit(header,' ');
species_name = (x{1});
% getting coefficients from first line
line_1 = fgetl(f1);
l1 = strfind(line_1,'E');
c1 = str2num(line_1(1:(l1(1)+3)));
c2 = str2num(line_1((l1(1)+4):(l1(2)+3)));
c3 = str2num(line_1((l1(2)+4):(l1(3)+3)));
c4 = str2num(line_1((l1(3)+4):(l1(4)+3)));
c5 = str2num(line_1((l1(4)+4):(l1(5)+3)));
% getting coefficients from second line
line_2 = fgetl(f1);
l2 = strfind(line_2,'E');
c6 = str2num(line_2(1:(l2(1)+3)));
c7 = str2num(line_2((l2(1)+4):(l1(2)+3)));
c8 = str2num(line_2((l2(2)+4):(l1(3)+3)));
c9 = str2num(line_2((l2(3)+4):(l1(4)+3)));
c10 = str2num(line_2((l2(4)+4):(l1(5)+3)));
% getting coefficients from Third line
line_3 = fgetl(f1);
l3 = strfind(line_3,'E');
c11 = str2num(line_3(1:(l3(1)+3)));
c12 = str2num(line_3((l3(1)+4):(l3(2)+3)));
c13 = str2num(line_3((l3(2)+4):(l3(3)+3)));
c14 = str2num(line_3((l3(3)+4):(l3(4)+3)));
% creating folder for each species
mkdir(species_name);
% temperature range
temp = linspace(global_min_temp,global_max_temp,1000);
% universal gas constant
R = 8.314;
% calculation
for j = 1:length(temp);
if temp(j)< global_mid_temp
c_p = (c1+c2.*temp+c3.*temp.^2+c4.*temp.^3+c5.*temp.^4).*R;
h = (c1+(c2.*temp)./2+(c3.*temp.^2)./3+(c4.*temp.^3)./4+(c5.*temp.^4)./5+c6./temp).*temp.*R;
s = (c1*log(temp)+c2.*temp+(c3.*temp.^2)/2+(c4.*temp.^3)/3+(c5.*temp.^4)/4+c7).*R;
else
c_p = (c8+c9.*temp+c10.*temp.^2+c11.*temp.^3+c12.*temp.^4).*R;
h = (c8+(c9.*temp)./2+(c10.*temp.^2)./3+(c11.*temp.^3)./4+(c12.*temp.^4)./5+c13./temp).*temp.*R;
s = (c8*log(temp)+c9.*temp+(c10.*temp.^2)/2+(c11.*temp.^3)/3+(c12.*temp.^4)/4+c14).*R;
end
end
% plotting
% Temperature vs Entropy
figure(1)
plot(temp,s,'LineWidth',3,'Color','b');
title(species_name);
xlabel('Temperature');
ylabel('Entropy');
% Temperature vs Enthalpy
figure(2)
plot(temp,h,'LineWidth',3,'Color','y');
title(species_name);
xlabel('Temperature');
ylabel('Enthalpy');
% Temperature vs Specific heat
figure(3)
plot(temp,c_p,'LineWidth',3,'Color','g');
title(species_name);
xlabel('Temperature');
ylabel('Specific Heat');
%changing file path to save plots
cd(species_name);
% saving the plots
saveas(1,'Entropy.jpg');
saveas(2,'Enthalpy.jpg');
saveas(3,'Specific_heat.jpg');
% calculation of molecular mass
elements = ['H','C','N','O','A'];
atomic_weight = [1.00794 12.0107 14.0067 15.9994 39.948];
MM = 0;
for i = 1:length(species_name);
for j = 1:length(elements);
if strcmp(species_name(1),elements(j))
MM = MM + atomic_weight(j);
u=j;
end
end
n=str2double(species_name(1));
if n>1
MM = MM + atomic_weight(u)*(n-1);
end
end
sprintf(' Molecular Weight of %s:%f u',species_name,MM)
end
fclose(f1)
OUTPUTS:
Seperate folder for each species :
Molecular mass in command window :
