Project 1 - Parsing NASA thermodynamic data
Title : Parsing NASA thermodynamic data
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.
Methodology :
In this we have used following terms in equations
- High temp coefficient(b1,b2,b3,b4,b5,b6,b7)
- low temp coefficient (a1,a2,a3,a4,a5,a6,a7)
- Universal gas constant = R
- Global minimum
- Global mid
- Global maximum
1.Function to calculate Specific Heat
%Function for Specific heat
function c_p=specific_heat(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,temp,global_min,global_mid,global_max)
if temp<=global_mid
c_p=R*(a1+a2.*temp+a3.*(temp.^2)+a4.*(temp.^3)+a5.*(temp.^4))
else
c_p=R*(b1+b2.*temp+b3.*(temp.^2)+b4.*(temp.^3)+b5.*(temp.^4))
end
- In the above functio we have used High temp coefficient,low temp coefficient ,Universal gas constant,Global minimu,Global mi,Global maximum.
- Then we have used if else to check the condtion (temp,=global mid)
- And we have calculated Specific heat i.e c_p.
2. Function to calculate Enthalpy
%Function to calcuate enthalpy
function H=enthalpy(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,temp,global_min,global_mid,global_max)
if temp <=global_mid
H = R.*temp.*(a1+a2.*(temp./2)+a3.*((temp.^2)./3)+a4.*((temp.^3)./4)+a5.*((temp.^4)./5)+a6./temp)
else
H = R.*temp.*(b1+b2*(temp./2)+b3.*((temp.^2)./3)+b4.*((temp.^3)./4)+b5.*((temp.^4)./5)+b6./temp)
end
- In the above functio we have used High temp coefficient,low temp coefficient ,Universal gas constant,Global minimu,Global mi,Global maximum.
- Then we have used if else to check the condtion (temp,=global mid)
- And we have calculated Enthalpy i.e H
3.Function to calculate Entropy
%Function for entropy
function S =entropy(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,temp,global_min,global_mid,global_max)
if temp<=global_mid
S=R*(a1.*log(temp)+a2.*temp +a3.*(temp.^2)./2 +a4.*(temp.^3)./3+a5.*(temp.^4)./4+a7)
else
S=R*(b1.*log(temp)+b2.*temp +b3.*(temp.^2)./2 +b4.*(temp.^3)./3+b5.*(temp.^4)./4+b7)
end
- In the above functio we have used High temp coefficient,low temp coefficient ,Universal gas constant,Global minimu,Global mi,Global maximum.
- Then we have used if else to check the condtion (temp,=global mid)
- And we have calculated Entropy i.e S
4.Function to Plot
%Function to plot all elements
function plotting(c_p,H,S,temp,element_name)
cd(pwd)
dir_name=element_name;
mkdir(\'dir_name\');
%plotting temp vs specific heat
figure(1)
plot(temp,c_p,\'linewidth\',3)
title(sprintf(\'Specific Heat vs Temperature\',element_name))
xlabel(\'Temperature\')
ylabel(\'Specific Heat\')
figure(2)
plot(temp,H,\'linewidth\',3)
title(sprintf(\'Enthalpy vs Temperature\',element_name))
xlabel(\'Temperature\')
ylabel(\'Enthalpy\')
figure(3)
plot(temp,S,\'linewidth\',3)
title(sprintf(\'Entropy vs Temperature\',element_name))
xlabel(\'Temperature\')
ylabel(\'Entropy\')
cd(dir_name)
saveas(1,\'c_p_vs_temp.png\')
saveas(2,\'H_vs_temp.png\')
saveas(1,\'S_vs_temp.png\')
cd
end
In the Above Function we have used several commands
- mkdir - By this we can create new folder in current folder pwd.
- The 1st fig. Represents the plot Specific heat vs temperature.
- The 2st fig. Represents the plot Enthalpy vs temperature.
- The 3st fig. Represents the plot Entropy vs temperature.
- saveas command is used to save 3 fig. in the folders.
- cd.. command is used to switch back to original command.
5.Function to calculate molecular weight
%Function for calculating mo;ecular weight
function[w]=molecular_weight(element_name)
s={\'H\' \'C\' \'N\' \'O\' \'AR\'};
%Atomic weight
atomic_wt=[1 12 14 16 40];
w=0
for i=1:length(element_name)
for j= 1:lenght(s)
if strcmp(element_name(i),s(j))
w=w + atomic_wt(j);
%storing position
position = j;
end
end
n= str2num(element_name(i));
if n>1
w = w+atomic_wt(position)*(n-1);
end
end
end
6,Function to write molecular weight data
%writing molecular weight of elements
function writing_to_file(element_name,weight)
dir_name=element_name
mol_file= fopen(\'mol_wt.docx\',\'a+\')
fprintf(mol_file,\'Molecular weight\',dir_name,weight)
fclose(mol_file)
end
Using these all Function below is the Main Code
clear all
close all
clc
%Inputs
R=8.314
%Read the data file
f1=fopen(\'therm.dat\',\'r\');
%Read the first line of file
l1=fgetl(f1);
%Define the Global min ,mid and max temperatues
l2=fgetl(f1);
temperatures = strsplit(l2);
global_min = str2num(temperatures{2});
global_mid = str2num(temperatures{3});
global_max = str2num(temperatures{4});
%Temperature Range
temp = linspace(global_min,global_max,500);
%Skip the comments
for i=1:3
fgetl(f1);
end
%create loop for all 53 species
for i=1:53
l=fgetl(f1);
element_name=strtok(1);
%Read the three lines (7 high tem and low temp coeff.)
%coefficients
l1=fgetl(f1);
l2=fgetl(f1);
l3=fgetl(f1);
%find E position to extract coefficients
a=strfind(l1,\'E\');
b=strfind(l2,\'E\');
c=strfind(l3,\'E\');
%Extract high temp coeff.
a1=str2num(l1(1:a(1)+3));
a2=str2num(l1(a(1)+3:a(2)+3));
a3=str2num(l1(a(2)+4:a(3)+3));
a4=str2num(l1(a(3)+4:a(4)+3));
a5=str2num(l1(a(4)+4:a(5)+3));
a6=str2num(l2(1:b(1)+3));
a7=str2num(l2(b(1)+4:b(2)+3));
%similally extract low temp coeff
b1=str2num(l2(b(2)+4:b(3)+3));
b2=str2num(l2(b(3)+4:b(4)+3));
b3=str2num(l2(b(4)+4:b(5)+3));
b4=str2num(l3(1:c(1)+3));
b5=str2num(l3(c(1)+4:c(2)+3));
b6=str2num(l3(c(2)+4:c(3)+3));
b7=str2num(l3(c(3)+4:c(4)+3));
%calculate Specific Heat
c_p=specific_heat(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,temp,global_min,global_mid,global_max);
%calculate enthalpy
H=enthalpy(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,temp,global_min,global_mid,global_max);
%calculte entropy
S =entropy(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,temp,global_min,global_mid,global_max);
%plot
plotting(c_p,H,S,temp,element_name)
%calculate molecular weight
mol_wt(i)=molecular_weight(element_name)
%function
weight=mol_wt(i);
writing_to_file(element_name,weight)
end
fclose(f1)
The Above code is the main program code in this we have used several commands.
- The value of universal gas constant R is taken 8.314
- the given data is therm.dat file
- then we have opned the file using fopen command.
- the file is in read mode using keyword \'r\'
- Then read the 1st line of file using fgetl.
- The use fgetl command along with strsplit in oredr to split it into 3values Global min ,Global mi,Global maximum.
- Then convert them in to number using str2num command.
- Then define temp array using linspace command.
- the next 3 lines are comments so ignored them using for loop .
- then for loop is created to loop through the 53 species data given in file and try to calculate the values which are required.
- Then fgetl to read next line of file which is name of file.
- The name of element is stored in variable using strtok command.
- Then read next 3 line in file of high and low temp data of each species.
- The values which we have read are in string format so we have convert them into number using str2num command.
- Then low temp values are stored in (a1,a2,a3,a4,a5,a6,a7)
- And high temp values in (b1,b2,b3,b4,b5,b6,b7)
- The next line is function called Specific heat which is been calculated.
- The next line is function called Enthalpy which is been calculated.
- The next line is function called Entropy which is been calculated.
- The next line is function called plot which is use to plot graph.
- The next line is function called Molecular weigth which is been calculatedof different elements.
- The next line is function which writes the molecular weight of different elements.
- Then the file f1 is closed using fclose
Result:
the graph plot od one of the element O2 is given below.
All other Plots for remaining species and given below
Link : https://drive.google.com/file/d/1LE3mgmxvbpl6qRktx9e87BL_7FbGqQYS/view?usp=sharing