Project 1 - Parsing NASA thermodynamic data
AIM-
To write the code in MATLAB to parse the NASA thermodynamic properties,molecular weigth and plot nature of the thermodynamic properties of various species.
OBJECTIVES-
1.To write a function that ectracts the 14 co-efficent for a given species from the given species.
2.To write functions that calucate the enthalpy,entropy and specific heat using the NASA polynomials for the given species.
3.To write a function that plots the cp,enthalpy,and entropy of an input species.
4.To write a function that calucates the molecular the molecular weight of each species.
INTRODUCTION-
parsing is the process of breaking a data block into smaller chunks by following as set of rules,so that it can be more easily interpreted,managed,or transmitted by a computer.NASA came up with polynomial that can be used to evaluate thermodynamics properties such as cp,H and Susing polynomials.They have also documented the co-effient that are requried to evaluate these polynomials for 1000+ species.
COMMANDS-
1.Frist,in the program,the values of the universal gas constant R is given as 8.314 as an input.
2.The given data is then formatted as a therm.dat file.
3.Then use the 'fopen' command the file is used to open the file in the read mode specified by 'r' keyword in the 'fopen' command.
4.The file is read line by line by using the 'fget' command .The frist line represent the title if the file so it is ignored.
5.The next line has the global maxmimum values which is read by using 'fgetl' command and splits into the three values by using the 'strsplit' command and converted into numbers.
6.Then the temperature array is defined using 'linspace' command as 500 values between the global minimum and global maximum values.
7.The next three lines are comments in the file so they are ignored by using a 'for' loop to iterate for 3 values
8.Then a 'for' loop is created to loop througth the 53 species data given in the file and calculate the requried values.
9.Inside the loop,the 'fgetl' command is used to read the next line of the file which represents the name of the species and create the vectors of the 400 evenly spaced temperature points using 'linspace' commands and provied universial gas constant.
10.The name of the element is stored in a variable using 'strtok' command
11.Then the next 3 lines of the file is read which contains the high and low temperature data of the particular species.
12.The read lines are in the form of string so they are converted into numbers using the 'str2num' command and stored in specific variable so they can be using in the further calucations
13.The low temperature values are stored in variables a1,a2,a3,a4,a5,a6,a7 and the high temperature vales are stored in variables b1,b2,b3,b4,b5,b6,b7.
14.The next line is reference to a function called specific heat which calucates and returns the values of specific heat using the high and low temperature value as inputs
15.The next line is reference to a function called enthalpy which calucates and returns the values of enthalpy using the high and low temperature value as inputs
16.The next line is reference to a function called entropy which calucates and returns the values of entropy using the high and low temperature values as inputs
17.The next line is reference to afunction that plotting graphs using the inputs.
18.The next line is referance to afunction that write the molecular weigths of the different elements.
19.The next line is referance to a function that write the molecular weigths different elements to the file.
20 The next line saving the plots with the 'mkdir'to the element name of the file.
21The NASA thermodynamic data file is closed using the 'fclose' command.
FUNCTION TO CALCULATE THE SPECIFIC HEAT:-
The function uses the high temperature coefficent,low temperature coefficent ,universal gas constant,global max temperature,global min temperature,global mid temperature and temperature values of inputs parameters
`cp=R(a1+a2T+a3T^2+a4T^3+a5T^4)`
Then we use the 'if else' condition statement to check if the temp values are less than or equal to the global mid temperature values and select the appropriate equation with high and low temperature coefficents is used to calucate the specific heat value denoted by cp and returns that values
%function to calucate the specific heat
function cp=specific_heat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,T,R,global_mid_temp);
if T<=global_mid_temp
cp=R.*(b1+b2.*T+b3.*T.^2+b4.*T.^3+b5.*T.^4);
else
cp=R.*(a1+a2.*T+a3.*T.^2+a4.*T.^3+a5.*T.^4);
end
FUNCTION TO CALCULATE THE ENTHALPY:-
The function uses the high temperature coefficent,low temperature coefficent,universal gas constant,global max temperature,global min temperature,global mid temperature and temperature values of inputs parameters
`h=RT(a1+a2frac{T}{2}+a3frac{T^2}{3}+a4frac{T^3}{4}+a5frac{T^4}{5}+frac{a6}{T})`
Then we use the 'if else' condition statement to check if the temp values are less than or equal to the global mid temperature values and select the appropriate equation with high and low temperature coefficent is used to calucate the enthalpy values denoted by h and returns that values
% function to calucating the enthalpy
function h=enthalpy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,T,R,global_mid_temp);
if T<=global_mid_temp
h=R.*(b1.*T+(b2.*T.^2)./2+(b3.*T.^3)./3+(b4.*T.^4)./4+(b5.*T.^5)./5+b6);
else
h=R.*(a1.*T+(a2.*T.^2)./2+(a3.*T.^3)./3+(a4.*T.^4)./4+(a5.*T.^5)./5+a6);
end
FUNCTION TO CALCULATE THE ENTROPY:-
The function uses the high temperature coefficent,low temperature coefficent,universal gas constant,global max temperature,global min temperature,global mid temperature and temperature value of inputs perameters
`s=R(a1logT+a2T+a3frac{T^2}{2}+a4frac{T^3}{3}+a5frac{T^4}{4}+a7`
Then we use the 'if eles' condition statement to check if the temp values are less than or equal to the global mid temperature values and select the appropriate equation with high and low temperature coefficent is used to calucate the entropy values denoted by s and returns that values
%function to calucate the entropy
function s =entropy(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,T,R,global_mid_temp);
if T<=global_mid_temp
s=R.*(b1.*log(T)+b2.*T+(b3.*T.^2)./2+(b4.*T.^3)./3+(b5.*T.^4)./4+b7);
else
s=R.*(a1.*log(T)+a2.*T+(a3.*T.^2)./2+(a4.*T.^3)./3+(a5.*T.^4)./4+a7);
end
FUNCTION TO CALcUlATE THE MOLECULAR WEIGTH:-
This function for molecular weigth caluculation requires element name as input argument and it is saved in the variable 'element' and the output arguments is molecular weigth
1.A string array is created which contains the name of the atoms.
2.A variable atomic weigth is created which of respective atoms in the string array
3.Then used 'strcmp' command to compare the name of the element with the string array.
4.if there is a repetition of an element 'str2num'is used to find that and (n-1) is mulitipuled to atomic weigth of the element to calculate molecular weigth
%function to calucate molecular weigth
function molecular_wt=molecular_weight(element_name);
atoms={'H','C','N','O','Ar'};
fprintf('molecular weigth of');
fprintf(element_name);
fprintf(':');
atomic_weight=[1.008, 12.011, 14.999, 15.007, 39.948];
molecular_wt=0;
for i =1:length(element_name);
for j =1:length(atoms);
if strcmp(element_name(i),atoms(j));
molecular_wt=molecular_wt+atomic_weight(j);
position = j;
end
end
n=str2num(element_name(i));
if n>1;
molecular_wt=molecular_wt+atomic_weight(position)*(n-1);
end
end
fprintf('%d',molecular_wt);
disp(' ');
end
MAIN PROGRAM:-
clear all
close all
clc
%reading the data file
f1=fopen('THERMO.dat','r');
%reading the frist line of the file
first_line=fgetl(f1);
%getting the global max,min,mid temperature
global_temp=fgetl(f1);
temp_range=strsplit(global_temp,' ');
global_min_temp=str2num(temp_range{2});
global_mid_temp=str2num(temp_range{3});
global_max_temp=str2num(temp_range{4});
%Defining the temperature ranges
T=linspace(global_min_temp,global_max_temp,400);
R=8.314;
%comments of the file
comment1=fgetl(f1);
comment2=fgetl(f1);
comment3=fgetl(f1);
%reading the three lines which contains 7 high temp cofficents & 7 low temp
%cofficents and position of E
for i =1:53
line1=fgetl(f1);
element_name=strtok(line1);
line2=fgetl(f1);
A=findstr(line2,'E');
line3=fgetl(f1);
B=findstr(line3,'E');
line4=fgetl(f1);
C=findstr(line4,'E');
%extracting high temp cofficents
a1=str2num(line2(1:A(1)+3));
a2=str2num(line2(A(1)+4:A(2)+3));
a3=str2num(line2(A(2)+4:A(3)+3));
a4=str2num(line2(A(3)+4:A(4)+3));
a5=str2num(line2(A(4)+4:A(5)+3));
a6=str2num(line3(1:B(1)+3));
a7=str2num(line3(B(1)+4:B(2)+3));
%extracting low temp cofficents
b1=str2num(line3(B(2)+4:B(3)+3));
b2=str2num(line3(B(3)+4:B(4)+3));
b3=str2num(line3(B(4)+4:B(5)+3));
b4=str2num(line4(1:C(1)+3));
b5=str2num(line4(C(1)+4:C(2)+3));
b6=str2num(line4(C(2)+4:C(3)+3));
b7=str2num(line4(C(3)+4:C(4)+3));
%calucating specific heat
cp=specific_heat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,T,R,global_mid_temp);
%calucating enthalpy
h=enthalpy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,T,R,global_mid_temp);
%calucating entropy
s =entropy(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,T,R,global_mid_temp);
%caluclating the molecular weigth of species
molecular_wt(i)=molecular_weight(element_name);
%read & write molecular list
molecular_list=fopen('molecular weigth.tex','w');
fprintf(molecular_list,'molecular weigth of %s is %d n',element_name,molecular_wt)
fclose(molecular_list);
%saving and plotting the data
mkdir(element_name);
cd (element_name);
%plotting
figure(1)
plot(cp,T,'linewidth',3,'color','r');
title(sprintf('specific heat v/s temperature,for %s',element_name));
xlabel('Temperature');
ylabel('specific heat');
grid on;
saveas(1,'specific_heat','png');
figure(2)
plot(h,T,'linewidth',3,'color','b');
title(sprintf('enthalpy v/s temperature for %s',element_name));
xlabel('temprature');
ylabel('enthalpy');
grid on
saveas(2,'enthaply','png');
figure(3)
plot(s,T,'linewidth',3,'color','g');
title(sprintf('entropy v/s temperature for %s',element_name));
xlabel('temperature');
ylabel('entropy');
grid on
saveas(3,'entropy','png');
cd ..
%molecular weight
molecular_wt=molecular_weight(element_name)
end
fclose(f1);
* For molecular weigth calculation create a funtion which takes weigth of hydrogen,carbon,nitrogen,and Argon as inputs.As all the element consists of all these atoms
* Now,I have created a loop and used 'strcmp'command to compare elements with species.if they both match then the molecular weigth will be calucated.
* Now,take the string of species name and position to calucalate their weigth.Here 'n' is number of molecules of elements present in the species.
PLOTTING AND SAVING THE PLOTS IN INDIVIDUAL FOLDERS:-



plots of o2:-



plots of N2:-



plots of co2:-



syntax used during coding
fopen:
open a file or obtain information about open files
r:
open the file for reading
fgetl:
read line from file
strsplit:
split string or character vector at specified delimiter
str2num
convert character array or string to numeric array
strfind:
find one string within another
linspace:
generate linearly spaced vectors
fprintf:
write formatted data to file
pwd
identify current directory
mkdir:
make new directory
cd:
change working
output:






















REFERANCE-
https://in.mathworks.com/help/matlab/ref/deblank.html
https://in.mathworks.com/help/matlab/ref/mkdir.html?searchHighlight=mkdir&s_tid=doc_srchtitle