Project 1 - Parsing NASA thermodynamic data
Aim:
To Write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.
Program:
% Parsing NASA thermodynamic data
clear all
close all
clc
% Input for the Program
R=8.314;
% opening and reading the file
f1=fopen('THERMO.dat','r');
get=fgetl(f1);
% Temperature reading
temp=fgetl(f1);
% Spliting
s=strsplit(temp);
% Converting string cells into numbers
temp_low=str2double(s{2});
temp_mid=str2double(s{3});
temp_high=str2double(s{4});
% Temperature Range
T=linspace(temp_low,temp_high,1000);
fgetl(f1)
fgetl(f1)
fgetl(f1)
% Skipping comment lines
for i=1:53
tline=fgetl(f1);
% splitting into cell array to read the name of species
A=strsplit(tline,' ');
species=(A{1});
% COnverting string to number
low_temp=str2double(A{end-3});
mid_temp=str2double(A{end-2});
high_temp=str2double(A{end-1});
tline1=fgetl(f1);
a=strfind(tline1,'E');
% Finding First 7 higher coefficients
a1=tline1(1:a(1)+3);
a1=str2double(a1);
a2=tline1(a(1)+4:a(2)+3);
a2=str2double(a2);
a3=tline1(a(2)+4:a(3)+3);
a3=str2double(a3);
a4=tline1(a(3)+4:a(4)+3);
a4=str2double(a4);
a5=tline1(a(4)+4:a(5)+3);
a5=str2double(a5);
tline2=fgetl(f1);
b=strfind(tline2,'E');
a6=tline2(1:b(1)+3);
a6=str2double(a6);
a7=tline2(b(1)+4:b(2)+3);
a7=str2double(a7);
% Finding 7 lower coefficients
a8=tline2(b(2)+4:b(3)+3);
a8=str2double(a8);
a9=tline2(b(3)+4:b(4)+3);
a9=str2double(a9);
a10=tline2(b(4)+4:b(5)+3);
a10=str2double(a10);
tline3=fgetl(f1);
c=strfind(tline3,'E');
a11=tline3(1:c(1)+3);
a11=str2double(a11);
a12=tline3(c(1)+4:c(2)+3);
a12=str2double(a12);
a13=tline(c(2)+4:c(3)+3);
a13=str2double(a13);
a14=tline3(c(3)+4:c(4)+3);
a14=str2double(a14);
for K=1:length(T)
% specific heat
Cp(K)=specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T(K),R,temp_mid);
% Entropy
S(K)= Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T(K),R,temp_mid);
% Enthalpy
H(K)= Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T(K),R,temp_mid);
end
% Molecular weight of the Species
W=Molecular_weight(species);
% writing molecular weight file
MW_file=fopen('Molecular Weight.txt','W');
fprintf(MW_file,'Molecular wieght of %s is %d',species,W);
fclose(MW_file);
% Saving in a folder
mkdir('something')
cd('something')
mkdir(species);
cd(species);
% plot1 Sp. heat(Cp) vs temp (T)
figure(1)
plot(T,Cp,'linewidth',2,'color','b');
xlabel('Temperature(K)');
ylabel('specific Heat(KJ)');
title(sprintf('Specific heat with Temperature range of %s',species));
grid on
saveas(figure(1),'Specific heat.jpg');
% plot2 Entropy(S) vs Temperature
figure(2)
plot(T,S,'linewidth',2,'color','r');
xlabel('Temperature (K)');
ylabel('Entropy(KJ/(Mol-K)');
title(sprintf('Entropy with Temperature range of %s',species));
grid on
saveas(figure(2),'Entropy.jpg');
% plot3 Enthalpy(H) vs temp(T)
figure(3)
plot(H,T,'linewidth',2,'color','g');
xlabel('Temperature (K)');
ylabel('Enthalpy (KJ/mol)');
title(sprintf('Enthalpy.jpg'));
grid on
saveas(figure(3),'Enthalpy.jpg');
cd ..
cd ..
% Molecular weight
W=Molecular_weight(species)
end
% Function for Specific heat
function Cp=specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,temp_mid)
if (T<=temp_mid)
Cp=R*(a8+(a9*T)+(a10*(T).^2)+(a11*(T).^3)+(a12*(T).^4));
else
Cp=R*(a1+(a2*T)+(a3*(T).^2)+(a4*(T).^3)+(a5*(T).^4));
end
end
% Funtion for Entropy
function S=Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,temp_mid)
if(T<= temp_mid)
S=R*(a8.*log(T)+(a9*T)+((a10*(T).^2)/2)+((a11*(T).^3)/3)+((a12*(T).^4)/4)+a14);
else
S=R*(a1.*log(T)+(a2*T)+((a3*(T).^2)/2)+((a4*(T).^3)/3)+((a5*(T).^4)/4)+a7);
end
end
Explaination:
- using fopen command,we open the file THERMO.dat, then input the parameter R.
- using fgetl command we read the THERMO file and with strsplit we split the temperatures.
- then we skip command lines using fgetl command inside the loop, then we will convert this string to number using str2num or str2double, to find low, high and mid temperatures.
- then to find the 53 element in the data file by assigning fgetl command and then spliting into the cell array to find the soecies line and then find the species name and temperature.
- we find the 'E' string using strfind command, to find the higher and lower number of coefficients.
- Now we define Specific heat Cp, Entropy S, Enthalpy H funtions and then specify molecular weight W function.
- using fprintf we define the molecular weight of the species, and then close the txt file using fclose command.
- using cd command we are creating directory folder, and save the files using mkdir command
- then polt the Cp, S, H with temperature of different species.
- then saveas to save the plots of different species.
Errors:
- Errors with variables while calling the entropy function.
- Error occured in spliting the Thermo file
- and the errors are been attached, please find it at the end.
Result:
The program to Write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file is completed and output is verified