Project 1 - Parsing NASA thermodynamic data
clear all
close all
clc
%%
% Reading the information from the file
% fpoen command is used to read the informatrion form a given file
f1= fopen('THERMO.dat','r');
% To read the informatin from the lines we use fgetl command.
r_line_1= fgetl(f1);
disp(r_line_1) % disp command is used to display the value of the varibale
% Similarly to read the second and third line information and so on we use
% the same command that is fgetl but change the imput for disp.
r_line_2_globaltemp = fgetl(f1);
disp(r_line_2_globaltemp)
% For skipping the next 3 lines we use for loop
for i= 1:3
r_line_skipped = fgetl(f1);
end
% Now we use srting split command to seperate the quantities with space
% delimiter
global_temprature_split=strsplit(r_line_2_globaltemp,' ');
% Strsplit command saves the varibale as a string so to change it to a
% number we use str2num command.
low_global_temperature_limit = str2num(global_temprature_split{2});
med_global_temperature_limit = str2num(global_temprature_split{3});
high_global_temperature_limit = str2num(global_temprature_split{4});
%%
% TO read the name of the species.
% There are 53 species so we use a for loop
for j= 1:53
% Reading the first line of species
line_1 = fgetl(f1);
% Splitting the first line using command strsplit
A = strsplit(line_1,' ');
% Extracting the species name
name_species = (A{1});
disp(name_species)
% Find the position of G we use the command strfind
G = strfind(line_1,'G');
% extracting the local temperatures using strsplit commmand
localtemp = line_1(G+4:length(line_1));
temp = strsplit(localtemp,' ');
% str2double command has been used to extrct local temperatures
low_local_temp = str2double(temp{end-3});
mid_local_temp = str2double(temp{end-1});
high_local_temp = str2double(temp{end-2});
% Extracting coeff from the first line that is from a1 to a5
line_2 = fgetl(f1);
% Finding position of E using strfind. strfind
E= strfind(line_2,'E');
% Extracting the coeff form 2nd line i.e from a1 to a5 usinf str2num
% command. str2num converts character array or string to numeric array.
% This will be done for each line containig variables.
a1 = str2num(line_2(1:E(1)+3));
a2 = str2num(line_2(E(1)+4:E(2)+3));
a3 = str2num(line_2(E(2)+4:E(3)+3));
a4 = str2num(line_2(E(3)+4:E(4)+3));
a5 = str2num(line_2(E(4)+4:E(5)+3));
% Extracting the coeff form 3nd line i.e from a6 to a10
line_3 = fgetl(f1);
% find E position
E_1 = strfind(line_3,'E');
a6 = str2num(line_3(1:E_1(1)+3));
a7 = str2num(line_3(E_1(1)+4: E_1(2)+3));
a8 = str2num(line_3(E_1(2)+4: E_1(3)+3));
a9 = str2num(line_3(E_1(3)+4: E_1(4)+3));
a10 = str2num(line_3(E_1(4)+4: E_1(5)+3));
% Extracting the coeff form 3nd line i.e from a11 to a14
line_4 = fgetl(f1);
% find E position
E_2 = strfind(line_4,'E');
a11 = str2num(line_4(1:E_2(1)+3));
a12 = str2num(line_4(E_2(1)+4: E_2(2)+3));
a13 = str2num(line_4(E_2(2)+4: E_2(3)+3));
a14 = str2num(line_4(E_2(3)+4: E_2(4)+3));
%%
T = linspace(low_local_temp,high_local_temp,500);
R = 8.314; % universal gas constant
% functions has been created for enthaly, entropy,specific heat and
% moleculsr weight
%Calculating enthaly, entropy, MW and Specific heat
h=enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,low_local_temp,mid_local_temp,high_local_temp,T,R);
s=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,low_local_temp,mid_local_temp,high_local_temp,T,R);
cp=specificheat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,low_local_temp,mid_local_temp,high_local_temp,T,R);
Mol_wt=MW(name_species);
disp(Mol_wt)
% Saving the folder
cd('D:\skill lync\challenge Parising\THermo');
mkdir(name_species); % This command will make the directory to save all the plots in the THermo folder automatically.
cd(name_species);
%plotting
figure(1)
plot(T,cp,'LineWidth',2,'Color','b');
title('Specific heat Plot of ', name_species)
xlabel('TEMPERATURE [ k ]' );
ylabel('SPECIFIC HEAT[ j/mol-k ]');
grid on
saveas(figure(1),'cp.jpg'); % this command has been used to save the figure in JPG format.
figure(2)
plot(T,h,'LineWidth',2,'Color','g');
title('Enthalpy Plot of ', name_species)
xlabel('TEMPERATURE [ k ]' );
ylabel('ENTHALPY[ j/kg ]');
grid on
saveas(figure(2),'H.jpg');
figure(3)
plot(T,s,'LineWidth',2,'Color','r');
title('Entropy Plot of ', name_species)
xlabel('TEMPERATURE [ k ]' );
ylabel('ENTROPY [ j/k ]');
grid on
saveas(figure(3),'S.jpg');
cd('D:\skill lync\challenge Parising\THermo');
% Writing the file for molecular weight using commands fopen and fprintf
% and then closing with fclosef.
MW_file = fopen('MW.txt','W');
fprintf(MW_file,'MW of %s is %d',name_species,Mol_wt);
fclose(MW_file);
end
fclose(f1)
FUNCTIONS
Enthalpy function
function[h]=enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,low_local_temp,mid_local_temp,high_local_temp,T,R)
if (T>mid_local_temp);
h=R*(a1*T+(a2*(T.^2)/2)+(a3*(T.^3)/3)+(a4*(T.^4)/4)+(a5*(T.^5)/5)+a6);
else
h=R*(a8*T+(a9*(T.^2)/2)+(a10*(T.^3)/3)+(a11*(T.^4)/4)+(a12*(T.^5)/5)+a13);
end
Entropy function
function[s]=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,low_local_temp,mid_local_temp,high_local_temp,T,R)
if (T>mid_local_temp);
s=(a1*log(T)+(a2*T)+(a3*(T.^2)/2)+(a4*(T.^3))+(a5*(T.^4)/4)+a7);
else
s=R*(a8*log(T)+(a9*T)+(a10*(T.^2)/2)+(a11*(T.^3))+(a12*(T.^4)/4)+a14);
end
Specific heat function
function[cp]=specificheat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,low_local_temp,mid_local_temp,high_local_temp,T,R)
if (T>mid_local_temp);
cp=R*(a1+(a2*T)+(a3*(T.^2))+(a4*(T.^3))+(a5*(T.^4)));
else
cp=R*(a8+(a9*T)+(a10*(T.^2))+(a11*(T.^3))+(a12*(T.^4)));
end
Molecular weight function
function[Mol_wt]=MW(name_species)
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(name_species)
for j=1:length(Elements)
if strcmpi(name_species(i),Elements(j))
Mol_wt= Mol_wt+Atomic_mass(j);
z=j;
end
end
var=str2double(name_species(i));
if (var>1)
Mol_wt=Mol_wt+(Atomic_mass(z)*(var-1));
end
end
end