Project 1 - Parsing NASA thermodynamic data
Objective:
- To develop a function that extracts the 14 coefficients and calculates the enthalpy, entropy, and specific heats for all the species in the data file.
- Develop a code to calculate the molecular weight of each species and display it in the command window.
-
To plot the Cp, Enthalpy, and Entropy for the local temperature range (low temperature: high temperature) specific for each species.
-
To save the plots as images with appropriate names and dump them in separate folders for each species with a suitable name. All of these should be generated automatically.
Principle:
NASA came up with polynomials that can be used to evaluate thermodynamic properties such as Cp, H, and S. They have also documented the coefficients that are required to evaluate these polynomials for 1000+ species.
Steps for choosing the logic of code and file parsing`
Step 1: - We open the file and parse the file ,fopen is used to open a file.
Step 2: - To navigate throughout the file and using (fgetl) command to get the line of the file and read the file line by line to get to the desired result.
Step 3: - Reading the text from the header file and read the file line by line
Step 4: - String split command then cell array is generated. A cell array is a special data structure to store data of the different type and perform the desired task
Step 5: - The function for Cp, H, S is created to calculate them by taking the values from the file data
Step 6: - Molecular weight is calculated using the molecular weight function and is printed on the command window
Step 7: - The plots for Cp, H, S are created for each species and saved in a folder in jpg format to access the species with different images
Step 8: - Creating folders for different molecules and save the plots for specific heat, enthalpy, and entropy in that folder and display the molecular weight of different species in the command window
MAIN CODE
%clc is for clearing the command window
%clear all is for clearing all the workspace variables
%close all is for closing all the output window
clc
clear all
close all
%command to open a file in read mode
f1 = fopen('THERMO.dat','r');
% to read file line by line
first_line = fgetl(f1);
%R is the universal gas constant J/(mol-K)
R = 8.314;
%to save the line in the temp variable
temp = fgetl(f1);
%Reading text from a header file
%Reading the global temperature from the next line
%to split the line saved in temp variable when space(' ') is found cell array is generated 1x4 cell array
A = strsplit(temp,' ');
%Cell array is generated 1x4 cell array
glob_lowtemp = str2num(A{2});
glob_midtemp = str2num(A{3});
glob_hightemp =str2num(A{4});
%Temperature range
%command to generate the 1000 values between the range of temperature
Temperature = linspace(glob_lowtemp,glob_hightemp,1000);
%skipping the three comment lines
for j =1:3;
k =fgetl(f1);
end
%running for loop fopr 53 times as there are 53 species
for i = 1:53;
%Parsing of the species block
tline = fgetl(f1);
S =strsplit(tline,' ');
Species_name =S{1};
%Extracting the coefficients from first line
line2 =fgetl(f1);
x =findstr(line2,'E');
%Finding the 5 coefficient
x1 = line2(1:x(1)+3);
x1 = str2num(x1);
x2 = line2(x(1)+4:x(2)+3);
x2 = str2num(x2);
x3 = line2(x(2)+4:x(3)+3);
x3 = str2num(x3);
x4 = line2(x(3)+4:x(4)+3);
x4 = str2num(x4);
x5 = line2(x(4)+4:x(5)+3);
x5 = str2num(x5);
%line3 5coefficients
line3 = fgetl(f1);
x = findstr(line3,'E');
x6 = line3(1:x(1)+3);
x6 = str2num(x6);
x7 = line3(x(1)+4:x(2)+3);
x7 = str2num(x7);
x8 = line3(x(2)+4:x(3)+3);
x8 = str2num(x8);
x9 = line3(x(3)+4:x(4)+3);
x9 = str2num(x9);
x10 = line3(x(4)+4:x(5)+3);
x10 = str2num(x10);
%Line4 4coefficients
line4=fgetl(f1);
x = findstr(line4,'E');
x11 = line4(1:x(1)+3);
x11 = str2num(x11);
x12 = line4(x(1)+4:x(2)+3);
x12 = str2num(x12);
x13 = line4(x(2)+4:x(3)+3);
x13 = str2num(x13);
x14 = line4(x(3)+4:x(4)+3);
x14 = str2num(x14);
%Entropy Calculation
S = entropy_S(x1,x2,x3,x4,x5,x7,x8,x9,x10,x11,x12,x14,Temperature,glob_midtemp,R);
%Cp calculation is Specific Heat calculation
Cp = Specific_heat(x1,x2,x3,x4,x5,x8,x9,x10,x11,x12,Temperature,glob_midtemp,R);
%Enthalpy Calculation is H
H = enthalpy_H(x1,x2,x3,x4,x5,x6,x8,x9,x10,x11,x12,x13,Temperature,glob_midtemp,R);
%Molecular Weight calculation of different elements
mw(i) = mol_wt(Species_name);
%Reading and writinh of the molecular weight in the list of molecular weight
Moll_wt = fopen('Molecular Weight.txt','w');
fprintf(Moll_wt,'Molecular Weight of the species %s is %f ',Species_name,mw);
fprintf(Moll_wt,'/n');
fclose(Moll_wt);
%Creating a folder for saving the species with its properties
directory_curr = pwd;
mkdir(Species_name);
cd(Species_name);
%Plotting the graph in different plotting window
%plot 1 for the temperature and entropy
%plot 2 for the temperature and enthalpy
%plot 3 for the temperature and specific heat
%plot 1 Enthalphy and Temperaure
figure(1)
plot(Temperature,H,'linewidth',3,'color','b');
xlabel('Temperature in [K]');
ylabel('Enthalphy [KJ/mol]');
grid on
title(sprintf('Variation in Enthalphy wrt Temperature %s',Species_name));
saveas (figure(1),'Enthalhy.jpg');
%plot 2 Entropy and Temperature
figure(2)
plot(Temperature,S,'linewidth',3,'color','r');
xlabel('Temperature in [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 Specific Heat and Temperature
figure(3)
plot(Temperature,Cp,'linewidth',3,'color','g');
xlabel('Temperature in [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
CODE FOR MOLECULAR WEIGHT FUNCTION
function mw = mol_wt(sp_name)
%storing value
compound_name = (sp_name)
% creating an array of elements
key_at = ['C','H','O','N','A'];
%storing the atomic mss value in the array
value_at = [12.0107 1.00794 15.994 14.0067 39.948];
%asssigning 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))
%if above comparision is true value of molecular mass is
%calculated
mw =mw + value_at(j);
%storing the position for further use
pos = j;
end
end
%converting the srting to number
k = str2num(compound_name(i));
%comparing if the value of k is greater than executed the further statements
if(k>1)
%molecular mass calculation
mw = mw + value_at(pos)*(k-1);
end
end
%command to print the molecular masss of the molecule
fprintf('Molecular Weight of the species %s is %f',compound_name ,mw)
end
CODE FOR SPECIFIC HEAT FUNCTION
function [Cp] = Specific_heat(x1,x2,x3,x4,x5,x8,x9,x10,x11,x12,temp,gb_m,r)
%Checking for the temperaure
%The first 7 coefficients are high-temperature coefficients and the second 7 coefficients are low-temperature coefficients
if(temp<=gb_m)
Cp = r.*(x8 +(x9.*temp) +(x10*(temp).^2) + (x11*(temp).^3) + (x12*(temp).^4));
else
Cp = r.*(x1 +(x2.*temp) +(x3*(temp).^2) + (x4*(temp).^3) + (x5*(temp).^4));
end
CODE FOR ENTHALPY FUNCTION
function [H] = enthalpy_H(x1,x2,x3,x4,x5,x6,x8,x9,x10,x11,x12,x13,temp,gb_m,r)
%Checking for the temperaure
%The first 7 coefficients are high-temperature coefficients and the second 7 coefficients are low-temperature coefficients
if(temp<=gb_m)
H = r*temp.*(x8 + ((x9*temp)/2) + ((x10*(temp).^2)/3) + ((x11*(temp).^3)/4) + ((x12*(temp).^4)/5) + ((x13./temp)));
else
H = r*temp.*(x1 + ((x2*temp)/2) + ((x3*(temp).^2)/3) + ((x4*(temp).^3)/4) + ((x5*(temp).^4)/5) + ((x6./temp)));
end
CODE FOR ENTROPY FUNCTION
function [S] = entropy_S(x1,x2,x3,x4,x5,x7,x8,x9,x10,x11,x12,x14,temp,gb_m,r)
%Checking for the temperaure
%The first 7 coefficients are high-temperature coefficients and the second 7 coefficients are low-temperature coefficients
%comparing matrix values of the temperature generated from the linspace command with the global medium temperature
if(temp<=gb_m)
S = r.*(x8.*log(temp) +(x9.*temp) +(x10*((temp).^2)/2) + (x11*((temp).^3)/3) + (x12*((temp).^4)/4) + (x14));
else
S = r.*(x1.*log(temp) +(x2.*temp) +(x3*((temp).^2)/2) + (x4*((temp).^3)/3) + (x5*((temp).^4)/4) + (x7));
end
MOLECULAR WEIGHT OF EACH SPECIES











SCREENSHOT OF THE WINDOW WHERE ALL FOLDERS ARE SAVED

PLOT FOR ENTHALPY WITH RESPECT TO TEMPERATURE O2

PLOT FOR ENTROPY WITH RESPECT TO TEMPERATURE O2

PLOT FOR SPECIFIC HEAT WITH RESPECT TO TEMPERATURE O2

PLOT FOR ENTHALPY WITH RESPECT TO TEMPERATURE N2

PLOT FOR ENTROPY WITH RESPECT TO TEMPERATURE N2

PLOT FOR SPECIFIC HEAT WITH RESPECT TO TEMPERATURE N2

PLOT FOR ENTHALPY WITH RESPECT TO TEMPERATURE CO2

PLOT FOR ENTROPY WITH RESPECT TO TEMPERATURE CO2

PLOT FOR SPECIFIC HEAT WITH RESPECT TO TEMPERATURE CO2

ERRORS FACED DURING DEVELOPMENT OF CODE
unrecognized function on debugging it was due to wrong command name fgetl was used instead

On debugging was found was due to the missing of space in strsplit command

On debugging was found was due to the missing of space in strsplit command

On Debugging for the code found that the *(multiplication sign) was missing

On Debugging for the code found that the brackets were missing in the command

On Debugging for the code found that instead of saveas only save was used

REFERENCES
https://www.mathworks.com/matlabcentral/answers/196582-best-way-to-parse-text-file