Project 1 - Parsing NASA thermodynamic data
We have to
1. Write a function that extracts the 14 coefficients and calculates the enthalpy, entropy and specific heats for all the species in the data file
2. Calculate the molecular weight of each species and display it in the command window.
3. Plot the Cp, Enthalpy, and Entropy for the local temperature range (low temperature: high temperature) specific for each species.
4. 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.
5. The main program that you will submit will just take the "THERMO.dat" file as an input and generate a plot for Cp, H, and S.
Explanation of the program :
1. we know the formula
Therefore creating a function to calculate the Specific Heat of species at constant Pressure, Enthalpy, and Entropy so 'if' statement is used for equations so that if the temperature is below the medium temperature, then low-temperature coefficients are used otherwise, high-temperature coefficients are used.
% Function To Calculate Specific Heat of species at constant Pressure, Enthalpy, and Entropy
function [ Cp H S ] = Thermodynamic_property(A1,A2,A3,A4,A5,A6,A7,a1,a2,a3,a4,a5,a6,a7,T,global_mid_temp)
% Cp = Specific Heat of species at constant Pressure
% H = Enthalpy
% S = Entropy
% T = Temperature Range
% R = Universal Gas Constant
R = 8.314 ;
for i = 1: length(T)
if T(i) >= global_mid_temp
Cp = (A1+ (A2.*T) + (A3.*T.^2) + (A4.*T.^3) + (A5.*T.^4)) * R ;
H = (A1+(A2.*(T./2))+(A3.*(T.^2)./3)+(A4.*(T.^3)./4)+(A5.*(T.^4)./5)+(A6./T)).*(T.*R);
S = ((A1.* log(T))+(A2.*T)+(A3.*T.^2)+(A4.*T.^3)+(A5.*T.^4)+A7)* R ;
else
Cp =(a1+ (a2.*T) + (a3.*T.^2) + (a4.*T.^3) + (a5.*T.^4)) * R ;
H = (a1+(a2.*(T./2))+(a3.*(T.^2)./3)+(a4.*(T.^3)./4)+(a5.*(T.^4)./5)+(a6./T)).*(T.*R);
S = ((a1.*log(T))+(a2.*T)+(a3.*T.^2)+(a4.*T.^3)+(a5.*T.^4)+a7)* R ;
end
end
2. Now writing a function to calculate the molecular weight of each species to display it in the command window
first store the different atoms and there respective atomic weight and set the initial value for molecular mass to 0
using for loop from j=1 to the length of species and k = 1 to the length of atomic weight, now strcmp is used to compare the species and elements so the loop is repeated till the atomic mass of all the atoms is added. for repeating atoms str2double is used to convert the species into a numeric value and identify that number corresponding to the atom and store in a variable called m and now multiplying the atomic weight by n where n = m-1.
% function to calculate the molecular weight of each species to display it in the command window
function molecular_mass = Molecular_Weight(species)
atoms = {'H','C','N','O','A'};
atomic_weight = [1.0078 12.0107 14.0067 15.9949 39.9480]; %respective atomic weight
molecular_mass = 0;
for j = 1:length(species)
for k = 1:length(atomic_weight)
if (strcmp(species(j),atoms(k))==1) % for the species with atoms
molecular_mass = molecular_mass + atomic_weight(k);
rank=k; % to find the position of atoms in the species
end
end
% For the weights of the repeating atoms
m = str2num(species(j));
if m>1
n=m-1;
molecular_mass = molecular_mass + atomic_weight(rank)*(n);
end
end
end
3.now to get the input for functions we read the data from the Thermo file and extract the required data from the file. first, we read the global data from the file and then read the data of the different species using the loop in which we use the logic of finding specific distinguisher and reading the values with reference to the searched content as shown in code
% Program for Parsing NASA thermodynamic data
clc
clear all
close all
file = fopen('THERMO.dat','r');
%reading global temperature data
file_name = fgetl(file);
range = fgetl(file);
global_temp_range = strsplit(range,' ');
global_low_temp = str2num(global_temp_range{2});
global_mid_temp = str2num(global_temp_range{3});
global_high_temp = str2num(global_temp_range{4});
for i= 1:3
a= fgetl(file) ;
end
%pairsing file data
for j= 1:53
line_1 = fgetl(file);
L1 = strsplit(line_1,' '); %splitting the line
species = (L1{1});
G = strfind(line_1,'G '); %finding the g in the line
%finding local temperature ranges
local_low_temp = str2num(line_1(G+3:G+10));
local_high_temp = str2num(line_1(G+12:G+20));
local_mid_temp = str2num(line_1(G+22:G+30));
T = linspace(local_low_temp,local_high_temp,500);
%finding the E in a file using strfind
%storing the collecting the numbers using str2num
line_2 = fgetl(file);
L2 = strfind(line_2,'E');
A1 = str2num(line_2(1:L2(1)+3));
A2 = str2num (line_2(L2(1)+4:L2(2)+3));
A3 = str2num (line_2(L2(2)+4:L2(3)+3));
A4 = str2num (line_2(L2(3)+4:L2(4)+3));
A5 = str2num (line_2(L2(4)+4:L2(5)+3));
line_3 = fgetl(file);
L3 = strfind(line_3,'E');
A6 = str2num(line_3(1:L3(1)+3));
A7 = str2num (line_2(L3(1)+4:L3(2)+3));
a1 = str2num (line_2(L3(2)+4:L3(3)+3));
a2 = str2num (line_2(L3(3)+4:L3(4)+3));
a3 = str2num (line_2(L3(4)+4:L3(5)+3));
line_4 = fgetl(file);
L4 = strfind(line_4,'E');
a4 = str2num(line_4(1:L4(1)+3));
a5 = str2num (line_4(L4(1)+4:L4(2)+3));
a6 = str2num (line_4(L4(2)+4:L4(3)+3));
a7 = str2num (line_4(L4(3)+4:L4(4)+3));
% Function To Calculate Cp, Enthalpy, and Entropy
[Cp,H,S] = Thermodynamic_property(A1,A2,A3,A4,A5,A6,A7,a1,a2,a3,a4,a5,a6,a7,T,global_mid_temp);
% To create Folders with species name
Folder_name = sprintf('%d) [%s]',j, species);
current_dir = pwd ; % To specify the current directory
mkdir(Folder_name); % To creat a Folder
cd(Folder_name); % To load plots in that new folder
figure (1);
plot(T,Cp,'r','linewidth',3)
grid on
xlabel('Local Temperature');
ylabel('Specific Heat')
title('Specific Heat(Cp) Vs Temperature(T)')
IMAGE1 = sprintf(" Cp Vs T of [%s].png",species); % To give a name of the file
saveas(gca,IMAGE1)
figure (2);
plot(T,H,'b','linewidth',3)
grid on
xlabel('Local Temperature');
ylabel('Enthalpy')
title('Enthalpy(H) Vs Temperature(T)')
IMAGE2 = sprintf(" H Vs T of [%s].png",species);
saveas(gca,IMAGE2)
figure (3);
plot(T,S,'g','linewidth',3)
grid on
xlabel(' Local Temperature');
ylabel(' Entropy')
title(' Entropy(S) Vs Temperature(T) ')
IMAGE3 = sprintf(" S Vs T of [%s].png",species);
saveas(gca,IMAGE3)
cd(current_dir);% To give a path,return to current directory
% Function To Calculate Molecular Weight
MW = Molecular_Weight(species);
fprintf(' %d) Molecular Weight of [%s] is %d.',j,species,MW)
fprintf('n n') % To set output after 2 line interval
end
Results :
Folder created by the program
Graphs :
For O2
For N2
For CO2
Molecular weight of each species in cammand window :
Challenges faced :
1. To find the commands to save the file
2.unknown variables are corrected by using the correct variable
3.error occurred due to not using . while multiplication.