Project 1 - Parsing NASA thermodynamic data
CODE FOR Parsing NASA thermodynamic data
CODE FOR THE FUNCTIONS USED IN THE MAIN PROGRAM
ENTROPY
CODE FOR ENTROPY FUNCTION
% Function to calculate the Entropy function
function S = entropy(x1,x2,x3,x4,x5,x7,y1,y2,y3,y4,y5,y7,R,temp,global_mid)
if temp<=global_mid
S = R.*(x1.*log(temp) + x2.*(temp) + x3.*((temp.^2)./2) + x4.*((temp.^3)./3) ...
+ x5.*((temp.^4)./4) + x7);
else
S = R.*(y1.*log(temp) + y2.*(temp) + y3.*((temp.^2)./2) + y4.*((temp.^3)./3) ...
+ y5.*((temp.^4)./4) + y7);
end
EXPLANATION
- Functions for the entropy to make a program user friendly
- function S = entropy(x1,x2,x3,x4,x5,x7,y1,y2,y3,y4,y5,y7,R,temp,global_mid) The statement that together to perform a task that to make acode to implement .
- First, x coefficients are high temperature and y coefficients are low temperature
- if temp<=global_mid - comparing the matrix value of the temperature generated from the line space command with global medium temperature.
- The following line indicates the calculation of the entropy written to the equation.
- else statement to perform the task if the 'if' statement fails
- The following line indicates the calculation of the entropy written to the equation.
- To end the function using the end command
ENTHALPY
CODE FOR ENTHALPY FUNCTION
% Function to calculate the Enthalpy function
function H = enthalpy(x1,x2,x3,x4,x5,x6,y1,y2,y3,y4,y5,y6,R,temp,global_mid)
if temp<=global_mid
H = R*temp.*(x1 + x2.*(temp./2) + x3.*((temp.^2)./3) + x4.*((temp.^3)./4) ...
+ x5.*((temp.^4)./5) + x6./temp);
else
H = R*temp.*(y1 + y2.*(temp./2) + y3.*((temp.^2)./3) + y4.*((temp.^3)./4) ...
+ y5.*((temp.^4)./5) + y6./temp);
end
EXPLANATION
- Functions for the enthalpy to make a program user friendly
- function H = enthalpy(x1,x2,x3,x4,x5,x6,y1,y2,y3,y4,y5,y6,R,temp,global_mid) The statement that together to perform a task that to make acode to implement .
- First, x coefficients are high temperature and y coefficients are low temperature
- if temp<=global_mid - comparing the matrix value of the temperature generated from the line space command with global medium temperature.
- The following line indicates the calculation of the entropy written to the equation.
- else statement to perform the task if the 'if' statement fails
- The following line indicates the calculation of the entropy written to the equation.
- To end the function using the end command
SPECIFIC HEAT
CODE FOR SPECIFIC HEAT FUNCTION
% Function to calculate the specific heat function
function c_p = specific_heat(x1,x2,x3,x4,x5,y1,y2,y3,y4,y5,R,temp,global_mid)
if temp<=global_mid
c_p = R*(x1 + (x2.*temp) + (x3.*(temp).^2) + (x4.*(temp).^3) + (x5.*(temp).^4));
else
c_p = R*(y1 + (y2.*temp) + (y3.*(temp).^2) + (y4.*(temp).^3) + (y5.*(temp).^4));
end
EXPLANATION
- Functions for the specific heat to make a program user friendly
- function c_p = specific_heat(x1,x2,x3,x4,x5,y1,y2,y3,y4,y5,R,temp,global_mid) The statement that together to perform a task that to make acode to implement .
- First, x coefficients are high temperature and y coefficients are low temperature
- if temp<=global_mid - comparing the matrix value of the temperature generated from the line space command with global medium temperature.
- The following line indicates the calculation of the entropy written to the equation.
- else statement to perform the task if the 'if' statement fails
- The following line indicates the calculation of the entropy written to the equation.
- To end the function using the end command
MOLECULAR WEIGHT
CODE FOR MOLECULAR WEIGHT FUNCTION
% Function to calculate Molecular Weight
function mw = molecular_weight(sp_name)
% Storing value
compound_name = (sp_name)
% Creating an array of an element
key_at = ['C','H','O','N','A'];
% Storing atomic mss value in the array
value_at = [12.015 1.015 15.995 14.045 39.924];
% Assigning molecular mass as 0
mw = 0;
% Creating for loop to perform the task
for i = 1:length(compound_name)
for j = 1:length(key_at)
% Comparing the string value
if strcmp(compound_name(i),key_at(j))
mw = mw + value_at(j);
% Storing the position for further use
pos = j;
end
end
% Converting the string to a number
k = str2num(compound_name(i));
if (k>1)
% Molecular mass calculation
mw = mw + value_at(pos)*(k-1);
end
end
% Command to print Molecular mass of the molecule
fprintf('Molecular Weight of the species %s is %f' , compound_name,mw)
end
EXPLANATION
- % Function to calculate Molecular Weight - comments to make user friendly
- function mw = molecular_weight(sp_name)
- compound_name = (sp_name)-storing a value
- key_at = ['C','H','O','N','A']; - creating an array element
- value_at = [12.015 1.015 15.995 14.045 39.924]; - storing an atomic mass value in an array
- mw = 0; - assigning molecular mass as 0
- for i = 1:length(compound_name) - creating a for loop to execute till the element length
- for j = 1:length(key_at) - creating a for loop to execute till the key at array length
- if strcmp(compound_name(i),key_at(j)) - comparing the string value of the species name and the element in the array at key
- mw = mw + value_at(j); - molecular mass calculation
- pos = j; - storing the statement
- end - to terminate the if statement
- end - to terminate the inner for loop
- k = str2num(compound_name(I)); - converting string to a number
- if (k>1) - comment
- mw = mw + value_at(pos)*(k-1); - molecular mass calculation
- end - to terminate the if statement
- end - to terminate the outer for loop
- fprintf('Molecular Weight of the species %s is %f' , compound_name,mw) - to print species name and value
- end - to terminate the function
MAIN CODE - NASA FILE PARSING and PLOT
% File Parsing for a file
close all
clear all
clc
f1 = fopen('THERMO.dat','r');
first_line = fgetl(f1);
% R is a universal gas constant J/(mol-k)
R = 8.314;
temp = fgetl(f1);
% Reading a text from a header file
% Reading the global temperature from the next line
A = strsplit(temp,' ');
% Cell array is generated 1*4 cell array
global_low = str2double(A{2});
global_mid = str2double(A{3});
global_max = str2double(A{4});
% Temperature range
temp = linspace(global_low , global_max , 1000);
% Skipping the three comment lines
for i = 1:3
k = fgetl(f1);
end
% Running for loop for 53 times for 53 species
for i = 1:53
% Parsing for species block
tline = fgetl(f1);
S = strsplit(tline,' ');
species_name = S{1};
% Extracting coefficient from line 1
line2 = fgetl(f1);
x = strfind(line2,'E');
% Finding the 5 coefficients
x1 = line2(1:x(1)+3);
x1 = str2double(x1);
x2 = line2(x(1)+4:x(2)+3);
x2 = str2double(x2);
x3 = line2(x(2)+4:x(3)+3);
x3 = str2double(x3);
x4 = line2(x(3)+4:x(4)+3);
x4 = str2double(x4);
x5 = line2(x(4)+4:x(5)+3);
x5 = str2double(x5);
% Line3 coefficient
line3 = fgetl(f1);
x = strfind(line3,'E');
x6 = line3(1:x(1)+3);
x6 = str2double(x6);
x7 = line3(x(1)+4:x(2)+3);
x7 = str2double(x7);
y1 = line3(x(2)+4:x(3)+3);
y1 = str2double(y1);
y2 = line3(x(3)+4:x(4)+3);
y2 = str2double(y2);
y3 = line3(x(4)+4:x(5)+3);
y3 = str2double(y3);
% Line 4 coefficients
line4 = fgetl(f1);
x = strfind(line4,'E');
y4 = line4(1:x(1)+3);
y4 = str2double(y4);
y5 = line4(x(1)+4:x(2)+3);
y5 = str2double(y5);
y6 = line4(x(2)+4:x(3)+3);
y6 = str2double(y6);
y7 = line4(x(3)+4:x(4)+3);
y7 = str2double(y7);
% Entropy Calculation
S = entropy(x1,x2,x3,x4,x5,x7,y1,y2,y3,y4,y5,y7,R,temp,global_mid);
% Specific Heat Calculation
c_p = specific_heat(x1,x2,x3,x4,x5,y1,y2,y3,y4,y5,R,temp,global_mid);
% Enthalpy calculation
H = enthalpy(x1,x2,x3,x4,x5,x6,y1,y2,y3,y4,y5,y6,R,temp,global_mid);
% Molecular Weight Calculation
mw(i) = molecular_weight(species_name);
% Reading and return of molecular weight
Moll_wt = fopen('Molecular Weight.txt','w');
fprintf(Moll_wt,'Molecular Weight of species %s is %f',species_name,mw);
fprintf(Moll_wt,'/n');
fclose(Moll_wt);
% Plotting on the graph for species for the temperature and specific
% heat
directory_curr = pwd;
% Plotting the graph in different plotting windows
% Plot 1 for temperature and enthalpy
% Plot 2 for temperature and entropy
% Plot 3 for temperature and specific heat
% Plot 1 for Temperature and Enthalpy
figure(1)
plot(temp,H,'LineWidth',3,'Color','g');
xlabel('Temperature [K]');
ylabel('Enthalpy [KJ/mol]');
grid on
title (sprintf('Variation in Enthalpy wrt Temperature %s',species_name));
saveas(figure(1),'Enthalpy.jpg');
% Plot 2 for Temperature and Entropy
figure(2)
plot(temp,S,'LineWidth',3,'Color','b');
xlabel('Temperature [K]');
ylabel('Entropy [KJ/mol-k]');
grid on
title (sprintf('Variation in Entropy wrt Temperature %s',species_name));
saveas(figure(2),'Entropy.jpg');
% Plot 3 for Temperature and Specific Heat
figure(3)
plot(temp,c_p,'LineWidth',3,'Color','y');
xlabel('Temperature [K]');
ylabel('Specific Heat [KJ]');
grid on
title (sprintf('Variation in Specific Heat wrt Temperature %s',species_name));
saveas(figure(3),'Specific Heat.jpg');
cd(directory_curr);
end
EXPLANATION
close all - to close all output window
clear all - to clear all the workspace variable
clc - to clear the command window
f1 = fopen('THERMO.dat','r'); - command to open a file in a read mode
first_line = fgetl(f1); - to read the file line by line
R = 8.314; - constant variable
temp = fgetl(f1); - to save the line in temp variable
A = strsplit(temp,' '); - to split the line in a temp variable when ('') is found
global_low = str2double(A{2}); - assigning the global low temp
global_mid = str2double(A{3}); - assigning the global medium temp
global_max = str2double(A{4}); - assigning the global maximum temp
temp = linspace(global_low , global_max , 1000); - command to generate 1000 values between the range of the temperature
for i = 1:3 - for skipping the command line by running loop
k = fgetl(f1); - to read the command line
end - to terminate the for loop
for i = 1:53 - to parse the file and plot for 53 molecules in a line
tline = fgetl(f1); - reading the first line for the module
S = strsplit(tline,' '); - splitting the string when space found by the command
species_name = S{1}; - extracting the name of the molecule
line2 = fgetl(f1); - reading the first line of the coeffiecients
x = strfind(line2,'E'); - finding the string using the command
x1 = line2(1:x(1)+3); - extracting the x first coefficient of the line
x1 = str2double(x1); - converting the string coefficient extracting all above the numbers
x2 = line2(x(1)+4:x(2)+3); - extracting the x second coefficient of the line
x2 = str2double(x2); - converting the string coefficient extracting all above the numbers
x3 = line2(x(2)+4:x(3)+3); - extracting the x third coefficient of the line
x3 = str2double(x3); - converting the string coefficient extracting all above the numbers
x4 = line2(x(3)+4:x(4)+3); - extracting the x fourth coefficient of the line
x4 = str2double(x4); - converting the string coefficient extracting all above the numbers
x5 = line2(x(4)+4:x(5)+3); - extracting the x fifth coefficient of the line
x5 = str2double(x5); - converting the string coefficient extracting all above the numbers
line3 = fgetl(f1); - reading the line for the module
x = strfind(line3,'E'); - finding the string using the command
x6 = line3(1:x(1)+3); - extracting the x6 coefficient of the line
x6 = str2double(x6); - converting the string coefficient extracting all above the numbers
x7 = line3(x(1)+4:x(2)+3); - extracting the x7 coefficient of the line
x7 = str2double(x7); - converting the string coefficient extracting all above the numbers
y1 = line3(x(2)+4:x(3)+3); - extracting the y1 coefficient of the line
y1 = str2double(y1); - converting the string coefficient extracting all above the numbers
y2 = line3(x(3)+4:x(4)+3); - extracting the y2 coefficient of the line
y2 = str2double(y2); - converting the string coefficient extracting all above the numbers
y3 = line3(x(4)+4:x(5)+3); - extracting the y3 coefficient of the line
y3 = str2double(y3); - converting the string coefficient extracting all above the numbers
line4 = fgetl(f1); - reading the line for the module
x = strfind(line4,'E'); - finding the string using the command
y4 = line4(1:x(1)+3); - extracting the y4 coefficient of the line
y4 = str2double(y4); - converting the string coefficient extracting all above the numbers
y5 = line4(x(1)+4:x(2)+3); - extracting the y5 coefficient of the line
y5 = str2double(y5); - converting the string coefficient extracting all above the numbers
y6 = line4(x(2)+4:x(3)+3); - extracting the y6 coefficient of the line
y6 = str2double(y6); - converting the string coefficient extracting all above the numbers
y7 = line4(x(3)+4:x(4)+3); - extracting the y7 coefficient of the line
y7 = str2double(y7); - converting the string coefficient extracting all above the numbers
S = entropy(x1,x2,x3,x4,x5,x7,y1,y2,y3,y4,y5,y7,R,temp,global_mid);
% Specific Heat Calculation
c_p = specific_heat(x1,x2,x3,x4,x5,y1,y2,y3,y4,y5,R,temp,global_mid);
% Enthalpy calculation
H = enthalpy(x1,x2,x3,x4,x5,x6,y1,y2,y3,y4,y5,y6,R,temp,global_mid);
% Molecular Weight Calculation
mw(i) = molecular_weight(species_name);
directory_curr = pwd; - identify the current dictionary
% Plot 1 for temperature and enthalpy
% Plot 2 for temperature and entropy
% Plot 3 for temperature and specific heat
% Plot 1 for Temperature and Enthalpy
figure(1)
plot(temp,H,'LineWidth',3,'Color','g'); - plot between the temp and enthalpy
xlabel('Temperature [K]'); - labeling on x axis
ylabel('Enthalpy [KJ/mol]'); labeling on y axis
grid on - to make grid on
title (sprintf('Variation in Enthalpy wrt Temperature %s',species_name)); - to make heading
saveas(figure(1),'Enthalpy.jpg');
% Plot 2 for Temperature and Entropy
figure(2)
plot(temp,S,'LineWidth',3,'Color','b'); - plot between the temp and entropy
xlabel('Temperature [K]'); - labeling x axis
ylabel('Entropy [KJ/mol-k]'); - labeling y axis
grid on to make grid visible
title (sprintf('Variation in Entropy wrt Temperature %s',species_name)); to make title
saveas(figure(2),'Entropy.jpg');
% Plot 3 for Temperature and Specific Heat
figure(3)
plot(temp,c_p,'LineWidth',3,'Color','y'); to plot temp and specific heat
xlabel('Temperature [K]'); - labeling x axis
ylabel('Specific Heat [KJ]'); - labeling y axis
grid on - to make grid visible
title (sprintf('Variation in Specific Heat wrt Temperature %s',species_name)); - to make an title
saveas(figure(3),'Specific Heat.jpg');
cd(directory_curr); - to change dictionary
end - to terminate the for loop