PROJECT:1- Parsing NASA thermodynamic data
%. what is file Parsing ?
File parsing is nothing but examining the file and extracting some important data, store them and perform anykind of calculations based on its application. In this project, NASA thermodynamic has been parsed to find out specific heat(Cp), Enthalpy(H) and Entropy(S) for the given 53 species.
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
here,
a1 to a7 = High-Temperature Coefficients
a8 to a14 = Low-Temperature Coefficients
R =Universal gas constant
T= Temperature
Objective:
*. 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.
*.Calculate the molecular weight of each species and display it in the command window.
*. Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.
*. Save the plots as images with appropriate names and dump them in separate folders for each species with suitable name. All these should be generated automatically.
*. The main program that you will submit will just take the "THERMO.dat" file as an input and generate Cp, H and S plots.
%MAIN CODE
% Write a function that extracts the 14 co-efficients o2, N2 and Co2 species
clear all
close all
clc
% Universal Gas Constant
R = 8.314; % j/mol-K
% load the 'Thermo.data' file
f1 = fopen('THERMO.dat','r');
l_1 = fgetl(f1); % First line read of 'Thermo.data' file
l_2 = fgetl(f1); % Second line read of 'Thermo.data'file
temp_range =strsplit(l_2,' '); % Split the Global Temperature 'string into number'{Important Step}
global_low_temp= str2num(temp_range{2}); % global lower Limit
global_mid_temp = str2num(temp_range{3}); % global Mid Point
global_high_temp = str2num(temp_range{4}); % global higher Limit
n=100; % no.of grid points
T=linspace(global_low_temp,global_high_temp,n); % Global Temperature Range
% skip the 3 lines as it has basic heading and information.
l_3 =fgetl(f1);
l_4 =fgetl(f1);
l_5 =fgetl(f1);
% using the 'For Loop' for extract the '53 Species in Thermo.data file'
for i = 1:53
% write the monatomic oxygen for read the 'First Species'
tline=fgetl(f1); % line 6
A = strsplit(tline,' '); % split the 'String line'
name_of_species=(A{1}); % name of the species
low_temp=str2num(A{end-3}); % Local low temp limit
high_temp=str2num(A{end-2}); % Local high temp limit
mid_temp=str2num(A{end-1}); % Local mid temp point
local_temp=linspace(low_temp,high_temp,n); % taking the local temperature for plotting purpose
tline_1 = fgetl(f1); % first line of coefficients
a= findstr(tline_1,'E'); % Find the string in the 'coefficients'
a1=tline_1(1:a(1)+3); % take 1st coefficient in string
a1=str2num(a1); % convert string coefficient into the number
a2=tline_1(a(1)+4:a(2)+3);
a2=str2num(a2);
a3=tline_1(a(2)+4:a(3)+3);
a3=str2num(a3);
a4=tline_1(a(3)+4:a(4)+3);
a4=str2num(a4);
a5=tline_1(a(4)+4:a(5)+3);
a5=str2num(a5);
tline_2 = fgetl(f1); % second line of coefficients
a= findstr(tline_2,'E'); % Find the string in the 'coefficients'
a6=tline_2(1:a(1)+3);
a6=str2num(a6);
a7=tline_2(a(1)+4:a(2)+3);
a7=str2num(a7);
a8=tline_2(a(2)+4:a(3)+3);
a8=str2num(a8);
a9=tline_2(a(3)+4:a(4)+3);
a9=str2num(a9);
a10=tline_2(a(4)+4:a(5)+3);
a10=str2num(a10);
tline_3 = fgetl(f1); % Third line of coefficients
a= findstr(tline_3,'E'); % Find the string in the 'coefficients'
a11=tline_3(1:a(1)+3);
a11=str2num(a11);
a12=tline_3(a(1)+4:a(2)+3);
a12=str2num(a12);
a13=tline_3(a(2)+4:a(3)+3);
a13=str2num(a13);
a14=tline_3(a(3)+4:a(4)+3);
a14=str2num(a14);
% Find out the 'Specific Heat, Enthalpy, Entropy'using the Function code
[Cp] =specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,mid_temp);
[H] = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,mid_temp);
[S] = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,mid_temp);
% calculate the molecular weight of each species
[M_W] = molecular_weight(name_of_species); % Function code of 'Molecular Weight'
k=sprintf('Molecular Weight of %s=%f',name_of_species,M_W); %print statement
disp(k) % display statement in 'Command Window'
cd('D:\matlab program\project1\')
% create folder
%mkdir(name_of_species);
cd(name_of_species);
% plotting of local T vs cp, local T vs H, and local T vs S for all 53 species.
figure(1)
plot(local_temp,Cp,'linewidth',2,'color','r')
xlabel('Local Temperature (K)')
ylabel('Specific Heat (J/Kg-K)')
grid on
title ('Local Temperature vs Cp')
saveas(figure(1),'specific heat.jpg')
figure(2)
plot(local_temp,H,'linewidth',2,'color','b')
xlabel('Local Temperature (K)')
ylabel('Enthalpy (J/kg)')
grid on
title ('Local Temperature vs Enthalpy')
saveas(figure(2),'Enthalpy.jpg')
figure(3)
plot(local_temp,S,'linewidth',2,'color','g')
xlabel('Local Temperature (K)')
ylabel('Entropy (J/K)')
grid on
title ('Local Temperature vs Entropy')
saveas(figure(3),'Entropy.jpg')
cd ..
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%function code for Specific heat,Enthalphy,Entropy and molecular weight.
%1.Specefic_heat
function [Cp] =specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,mid_temp) % Specific Heat Function
if (T<=mid_temp) % 'if condition' based on the ' Local Mid temperature'
Cp = ((a8+(a9*T)+(a10*T.^2)+(a11*T.^3)+(a12*T.^4))*R);
else % Local high temperature
Cp = ((a1+(a2*T)+(a3*T.^2)+(a4*T.^3)+(a5*T.^4))*R);
end
%2.Enthalphy
function [H] = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,mid_temp) % Enthalpy function
if (T<=mid_temp) % 'if condition' based on the ' Local Mid temperature'
H = (a8*T+a9*(T.^2/2)+a10*(T.^3/3)+a11*(T.^4/4)+a12*(T.^5/5)+((a13))*R);
else % Local high temperature
H = (a1*T+a2*(T.^2/2)+a3*(T.^3/3)+a4*(T.^4/4)+a5*(T.^5/5)+((a6))*R);
end
%3.Entropy
function [S] = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,mid_temp) % Entropy Function
if (T<=mid_temp) % 'if condition' based on the ' Local Mid temperature'
S = ((a8*log(T)+(a9*T)+a10*(T.^2/2)+a11*(T.^3/3)+a12*(T.^4/4)+(a14))*R);
else % Local high temperature
S = ((a1*log(T)+(a2*T)+a3*(T.^2/2)+a4*(T.^3/3)+a5*(T.^4/4)+(a7))*R);
end
%4.Molecular_weight
function [M_W] = molecular_weight(name_of_species) % Molecular weight function
elements = ['H','C','O','N','AR']; % chemical elements in 'Thermo.data file' combination of these element
atomic_weight=[1 12 16 14 39]; % chemical element 'atomic weight'
M_W=0; % counter variable
for i = 1:length(name_of_species) % to read the 'entire species in file'
for j=1:length(elements) % to read the 'elements' in file
if strcmp(name_of_species(i),elements(j)) % condition statement 'if' species =element equal then only loop enter otherwise exist
M_W=M_W+atomic_weight(j); % molecular weight 'Summation', using counter variable (initial=0), atomic weight of above elements
A=j;% element array
end
end
n=str2num(name_of_species(i)); % write for 'no.of moles'
if(n>1) % if condition 'no.of moles >1'enter into loop otherwise exist
M_W=M_W+(atomic_weight(A)*(n-1));
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%MAIN CODE EXPLANATION
%. First of all, 'fopen' command is used to read the file inside the matlab.
%. As couple of lines are not useful for the project, it has been passed by using 'fgetl' command.
%. third line has global temp. values in form of string which is then coverted to num using 'str2num'.
%.Next three lines were only having general information so that has been skipped by the fgetl command.
%. now before executing the main code data the for loop has been used to repeat the cycle for 53 time.
%.Line 6 has species name and local temp that has been extrated using the 'findstr' command.
&.Line 7,8,9 (In the f1 file) has the required 14 coefficents for the calculations, by using fgetl and findstr all the 14 coefficents are extracted.
%.In order to calculate the molecular weight(M_W), Cp,H and S the funtion has been created for the repsective terms.
%. using 'mkdir' and 'cd' command the directory and newfolder has been created to store the outputs of the above terms and for graphs as well.