Project 1 - Parsing NASA thermodynamic data
Aim : to parse information from NASA Thermodynamic data file and use teh data to find properties such as cp, enthalpy,entropy.
Formula:
`(Cp)/R = a1 + a2 T + a3 T^2 + a4 T^3 + a5 T^4`
`H/(RT) = a1 + a2 T/2 + a3 T^2/3 + a4 T^3/4 + a5 T^4/5 + a6/T`
`S/R =a1 ln T+ a2 T + a3 T^2 /2 + a4 T^3 /3 + a5 T^4 /4 + a7`
CpR=a1+a2T+a3T2+a4T3+a5T
Main Code:
clear all
close all
clc
%Reading header
f1 = fopen('THERMO.dat','r');
R = 8.314 ;
%skipping to get to main part
for i=1:5
fgetl(f1);
end
%for loop total elements = 53
for i = 1:53
line = fgetl(f1); %Reading the line
a =strsplit(line,' '); %Seperating Variable
species = a(1);
p = strfind(line,'G');
local_low_temp = line(p+3:55);
local_high_temp = line(p+13:65);
local_mid_temp = line(p+23:75);
lt = str2double(local_low_temp); %Local Low Temp
ht = str2double(local_high_temp); %Local Mid Temp
mt = str2double(local_mid_temp); %Local High Temp
%Reading coefficients
cfline1= fgetl(f1);
t = strfind(cfline1,'E'); %exponent
a1 = cfline1(1:t(1)+3);
a1 = str2double(a1); %1st coeff
a2 = cfline1(t(1)+4:t(2)+3);
a2 = str2double(a2); %2nd coeff
a3 = cfline1(t(2)+4:t(3)+3);
a3 = str2double(a3); %3rd coeff
a4 = cfline1(t(3)+4:t(4)+3);
a4 = str2double(a4); %4th coeff
a5 = cfline1(t(4)+4:t(5)+3);
a5 = str2double(a5); %5th coeff
%line 2-Reading coefficient data of 2nd line
cfline2 = fgetl(f1);
y = strfind(cfline2,'E');
a6 = cfline2(1:y(1)+3);
a6 = str2double(a6); %6th coeff
a7 = cfline2(y(1)+4:y(2)+3);
a7 = str2double(a7); %7th coeff
a8 = cfline2(y(2)+4:y(3)+3);
a8 = str2double(a8); %8th coeff
a9 = cfline2(y(3)+4:y(4)+3);
a9 = str2double(a9); %9th coeff
a10 = cfline2(y(4)+4:y(5)+3);
a10 = str2double(a10); %10th coeff
%line 3-Reading coefficient data of 3rd line
cfline3 = fgetl(f1);
u = strfind(cfline3,'E');
a11 = cfline3(1:u(1)+3);
a11 = str2double(a11); %11th coeff
a12 = cfline3(u(1)+4:u(2)+3);
a12 = str2double(a12); %12th coeff
a13 = cfline3(u(2)+4:u(3)+3);
a13 = str2double(a13); %13th
a14 = cfline3(u(3)+4:u(4)+3);
a14=str2double(a14); %14th coeff
%low temperature to high temperature range
T=linspace(lt,ht,500);
%Specific Heat %Entropy %Enthaphy
Cp = specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt);
S = entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt);
H=enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt);
%Saving as a folder
spec=char(species);
fd = sprintf('%s',spec);
cur_dir=pwd;
mkdir(spec);
cd(spec);
%Plot 1, Specific heat (Cp) vs Temperature (T)
figure(1);
plot(T,Cp,'linewidth',2,'color','r');
xlabel('Temperature in K');
ylabel('Specific heat in kJ');
title('Specific heat vs Temperature');
%Plot 2, Enthalpy (H) vs Temperature (T)
figure(2);
plot(T,H,'linewidth',2,'color','b');
xlabel('Temperature in K');
ylabel('Enthalpy in kJ/(mol)');
title('Enthalpy vs Temperature');
%Plot 3, Entropy (S) vs Temperature (T)
figure(3);
plot(T,S,'linewidth',2,'color','g');
xlabel('Temperature in K');
ylabel('Entropy in kJ/(mol K)');
title('Entropy vs Temperature');
% saving the plots that got generated
saveas(figure(1),'cp.png');
saveas(figure(2),'h.png');
saveas(figure(3),'s.png');
cd(cur_dir);
end
fclose(f1);
Code For enthalpy
function H=enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt)
if T>=mt
H=(a1.*T+a2.*(T.^2)./2+a3.*(T.^3)./3+a4.*(T.^4)./4+a5.*(T.^5)./5+a6)*R;
else
H=(a8.*T+a9.*(T.^2)./2+a10.*(T.^3)./3+a11.*(T.^4)./4+a12.*(T.^5)./5+a13)*R;
end
Code for Entropy
function S=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt)
if T>=mt
S=(a1.*log(T)+a2.*T+a3.*(T.^2)./2+a4.*(T.^3)./3+a5.*(T.^4)./4+a7)*R;
else
S=(a8.*log(T)+a9.*T+a10.*(T.^2)./2+a11.*(T.^3)./3+a12.*(T.^4)./4+a14)*R;
end
Code for Specific Heat
function Cp=specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt)
if T>=mt
Cp=(a1+a2.*(T)+a3.*(T.^2)+a4.*(T.^3)+a5.*(T.^4))*R;
else
Cp=(a8+a9.*(T)+a10.*(T.^2)+a11.*(T.^3)+a12.*(T.^4))*R;
end
Usage Of various commands and logic:
1) fopen to open the Thermo.dat file by NASA
2) fgetl to read the file line by line
3) ignore header and global temp ranges as of no practical use in code
4)Using strsplit to split the string into a cell array so that individual coefficients can be recognized
5)str2num to convert string to variable
6) cd(pwd) and mkdir are used to open current directory and create new folder respectively.
Plots:
(1) O2:
(2) N2:
(3)CO2:
(4) SAVE FILE: