Project 1 - Parsing NASA thermodynamic data
Question:- Parsing NASA thermodynamic data ?
Answer:-
Theory:- There is a NASA thermodynamic data having different thermodynamic values w.r.t different temperature in polynomial form in '.dat' format also having 14 coefficients. In order to extract the different information we need to parse the using Matlab.
The term 'prasing' refers to MATLAB reading an m-file and translating it into an internal code that it stores in memory . It is this 'prased' file in memory that is actually run when you execute the code.
THE PROGRAMING PART:-
>>Function Code for molecular weight calculation for different species-
function Molecularweight = Molecular_weight(Species)
atomic_name = ['H','C','O','N','A'];
fprintf('Molecular Weight of ');
fprintf(Species);
fprintf(':');
Atomic_Weight = [1.008 12.011 15.999 14.007 39.948];
Molecularweight = 0;%Initiating Range of Molecule
%Adding Atomic number to get particular value
for i = 1:length(Species)
for j = 1:length(atomic_name)
if strcmpi(Species(i),atomic_name(j))
Molecularweight = Molecularweight+Atomic_Weight(j);
position = j;
end
end
n = str2num(Species(i));
if n>1
Molecularweight = Molecularweight+Atomic_Weight(position)*(n-1);
end
end
fprintf('molecular weight %s :',Species);
fprintf('%d',Molecularweight);
disp('');
end
THE MAIN CODE
close all
clear all
clc
f1 = fopen('THERMO.dat','r');
fgetl(f1);
%Defining temperature values
templ = str2num(fgetl(f1));
global_low_temp = templ(1);
global_mid_temp = templ(2);
golbal_high_temp = templ(3);
%For skipping unwanted lines
for i = 1:3
fgetl(f1);
end
for i=1:53
line1 = fgetl(f1);
l = strsplit(line1,'');
m = length(l);
species = l{1};
%Storing the value of the local temperature values
low_temp = str2double(l(m-3));
high_temp = str2double(l(m-2));
mid_temp = str2double(l(m-1));
%Storing the value of the second line of variables
line2 = fgetl(f1);
%Finding the position of E
a = strfind(line2,'E');
%Reading all the other variables
a1 = str2double(line2(1:(a(1)+3)));
a2 = str2double(line2((a(1)+4):(a(2)+3)));
a3 = str2double(line2((a(2)+4):(a(3)+3)));
a4 = str2double(line2((a(3)+4):(a(4)+3)));
a5 = str2double(line2((a(4)+4):(a(5)+3)));
%Storing the values of the 3rd line
line3 = fgetl(f1);
%Finding the position of E
b = strfind(line3,'E');
%Storing the values for the next set of variables
a6 = str2double(line3(1:b(1)+3));
a7 = str2double(line3((b(1)+4):(b(2)+3)));
a8 = str2double(line3((b(2)+4):(b(3)+3)));
a9 = str2double(line3((b(3)+4):(b(4)+3)));
a10 = str2double(line3((b(4)+4):(b(5)+3)));
%Storing ther values of the 4th line
line4 = fgetl(f1);
%finding the position of E
c = strfind(line4,'E');
%Storing the values of variables in line 4
a11 = str2double(line4(1:c(1)+3));
a12 = str2double(line4((c(1)+4):(c(2)+3)));
a13 = str2double(line4((c(2)+4):(c(3)+3)));
a14 = str2double(line4((c(3)+4):(c(4)+3)));
%Creating Temperature values for lower and higher temperature
lower_temp = linspace(low_temp,mid_temp,100);
higher_temp = linspace(mid_temp,high_temp,100);
%Value of the universal has constant in j/mol*K
R = 8.314;
%calculating the values for cp,H and S using the given formulas
for k = 1:100
% lower temperature values for cp,h,s calculations i.e low to
% middle temp values
cp_low(k)=(a8+(a9*lower_temp(k))+(a10*(lower_temp(k)^2))+(a11*(lower_temp(k))^3)+(a12*(lower_temp(k))^4))*R;
h_low(k)=(a8+(a9*lower_temp(k))/2+(a10*lower_temp(k)^2)/3+(a11*lower_temp(k)^3)/4+a12*(lower_temp(k)^4)/5+(a13/lower_temp(k)))*R*lower_temp(k);
s_low(k)=((a8*log(lower_temp(k))+(a9*lower_temp(k))+((a10*lower_temp(k)^2)/2)+((a11*lower_temp(k)^3)/3)+((a12*lower_temp(k)^4)/4)+a14)*R);
%Higher temperature values for co,h,s calculations i.e mid to high
%temp values
cp_high(k)=(a1+(a2*higher_temp(k))+(a3*(higher_temp(k)^2)+(a4*(higher_temp(k))^3)+a5*(higher_temp(k))^4))*R;
h_high(k)=(a1+(a2*higher_temp(k))/2+(a3*higher_temp(k)^2)/3+(a4*higher_temp(k)^3)/4+(a5*higher_temp(k)^4))/5+(a6/higher_temp(k))*R*higher_temp(k);
s_high(k)=((a1*log(higher_temp(k))+a2*higher_temp(k)+((a3*higher_temp(k)^2)/2)+((a4*higher_temp(k)^3)/3)+((a5*higher_temp(k)^4)/4)+a7)*R);
end
%Molecular weight calculation
Molecularweight(i) = molecular_weigh(species);
% Maring folder for saving the plots
nasa_graphs = species;
mkdir(nasa_graphs);
%Plotting the graphs
%For specific heat
figure(1)
plot(lower_temp,cp_high,'LineWidth',3,'Color','r');
hold on
plot(higher_temp,cp_high,'LineWidth',3,'Color','r');
hold off
title(['Temperature vs Specific Heat',species]);
xlabel('Temperature');
ylabel('Specific Heat');
legend('low temperature','high temerature');
fig = sprintf('Secific Heat of %s.png',species);
saveas(gca,fig);
%For Enthalpy
figure(2)
plot(lower_temp,h_low,'LineWidth',2,'Color','c');
hold on
plot(higher_temp,h_high,'LineWidth',2,'Color','c');
hold off
title(['Temperature vs Enthalpy',species]);
xlabel('Temperature');
ylabel('Enthalpy');
legend('lowtemprature','high temprature');
fig = sprintf('Enthalpy of %s.png',species);
saveas(gca,fig);
%For Enthalpy
figure(3)
plot(lower_temp,s_low,'LineWidth',3,'Color','y');
hold on
plot(higher_temp,s_high,'LineWidth',3,'Color','y');
hold off
title(['Temperature vs Entropy',species]);
xlabel('Temperature');
ylabel('Entropy');
legend('low temprature','high temprature');
fig = sprintf('Entropy of %s.png',species);
saveas(gca,fig);
cd(current)
end
THE CODE EXPLANATION
>>First write the molecular weight function to call up it in the main program to display it in command window.
>> Read the NASA thermodynamic data by using fopen to open and r command to read it.
>> Extract the global temperature and store it.
>>TO extract the local temprature form the file.
>>Read all the coefficient of NASA data file from a1 to a14 and covert that string to number using str2double.
>>make a folder named as nasa data to store all the graphs .
>>Calculate cp,H,S by the thermodynamic relation and molecular weight bty calling the function.
>>Plot and save the fig. in png formate using saveas command.
>>Molecular weight will be displayed in command window.