% calculating molecular weight
function mw= molecular_wt(species_name)
molecule = species_name;
%storing the atoms required for calculation
atoms = ['C','H','O','N','A'];
%storing corresponding atomic mass value in an array
mass = [12.011 1.0079 15.994 14.0067 39.948];
mw=0;
for i= 1:length(molecule)
for j= 1:length(atoms)
if strcmpi(molecule(i),atoms(j));
mw=mw+mass(j);
% storing the position
pos=j;
end
end
%calculating the number by converting string to number
% since it is cell array use {}
k = str2num(molecule(i));
%if the value od k>1 it is already executed one time,it has to add
%(k-1)times
if(k>1)
mw=mw+(mass(pos)*(k-1));
end
end
%enthalpy function
function [hval] = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,temp_R,l_temp_med,R);
% a1 to a7 high temperature coefficient & a8 to a14 low temp coefficient
if (temp_R<=l_temp_med)
hval= R*temp_R.*(a8+(a9.*(temp_R)/2)+(a10*(temp_R).^2)/3+(a11*(temp_R).^3)/4+(a12*(temp_R).^4)/5+a13./temp_R);
else
hval= R*temp_R.*(a1+(a2.*(temp_R)/2)+(a3*(temp_R).^2)/3+(a4*(temp_R).^3)/4+(a5*(temp_R).^4)/5+a6./temp_R);
end
%entropy function
function [sval] =entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,temp_R,l_temp_med,R);
% a1 to a7 high temperature coefficient & a8 to a14 low temp coefficient
if (temp_R<=l_temp_med)
sval = R.*((a8.*log(temp_R))+(a9.*(temp_R))+(a10.*((temp_R).^2)/2)+(a11.*((temp_R).^3)/3)+(a12.*((temp_R).^4))/4+a14);
else
sval = R.*((a1.*log(temp_R))+(a2.*(temp_R))+(a3.*((temp_R).^2)/2)+(a4.*((temp_R).^3)/3)+(a5.*((temp_R).^4))/4+a7);
end
% specific heat function
function [cpval] = specifi_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,temp_R,l_temp_med,R)
% a1 to a7 high temperature coefficient & a8 to a14 low temp coefficient
if (temp_R<=l_temp_med)
cpval= R*(a8+(a9.*temp_R)+ (a10.*temp_R.^2)+(a11.*temp_R.^3)+(a12.*temp_R.^4));
else
cpval= R*(a1+(a2.*temp_R)+ (a3.*temp_R.^2)+(a4.*temp_R.^3)+(a5.*temp_R.^4));
end
%main program for file parsing
clear all
close all
clc
%opening the data
data =fopen('thermo.dat','r');
%getting first line
line_heading = fgetl(data);
%getting the temperature line and splitting the global temperature
temperature = fgetl(data);
temp = strsplit(temperature,' ');
g_temp_low = str2num(temp{2});
g_temp_mid = str2num(temp{3});
g_temp_hig = str2num(temp{4});
% temperature range
temp_R=linspace(g_temp_low,g_temp_hig,1000);
% universal gas constant
R=8.314;
%skipping next three lines
for i= 1:3
fgetl(data);
end
% for loop is used for parsing data of 53 molecules
for i =1:53
a =fgetl(data);
line_data = strsplit(a,' ');
% reading the species name
species_name = line_data{1};
%reading the local temperature of species
l_temp_med = str2num(line_data{:,end-1});
l_temp_hig = str2num(line_data{:,end-2});
l_temp_low = str2num(line_data{:,end-3});
%extracting the coefficient of species
%first line
line_1 = fgetl(data);
%finding the position of E
x = findstr(line_1,'E');
%extracting thr coeff using the position of E
coeff1 = line_1(1:x(1)+3);
coeff2 = line_1(x(1)+4:x(2)+3);
coeff3 = line_1(x(2)+4:x(3)+3);
coeff4 = line_1(x(3)+4:x(4)+3);
coeff5 = line_1(x(4)+4:x(5)+3);
%converting the coefficients from string to number
a1 = str2num(coeff1);
a2 = str2num(coeff2);
a3 = str2num(coeff3);
a4 = str2num(coeff4);
a5 = str2num(coeff5);
% accessing the second line
line_2 = fgetl(data);
%same procedure is prepeated as in the case of line_1
%finding the position of E
x = findstr(line_2,'E');
coeff6 = line_2(1:x(1)+3);
coeff7 = line_2(x(1)+4:x(2)+3);
coeff8 = line_2(x(2)+4:x(3)+3);
coeff9 = line_2(x(3)+4:x(4)+3);
coeff10 = line_2(x(4)+4:x(5)+3);
% coverting string to number
a6 = str2num(coeff6);
a7 = str2num(coeff7);
a8 = str2num(coeff8);
a9 = str2num(coeff9);
a10 = str2num(coeff10);
% getting the the third line using fgetl
line_3 = fgetl(data);
%same procedure as of line_1 and line_2
%finding the position of E
x = findstr(line_3,'E');
coeff11 = line_3(1:x(1)+3);
coeff12 = line_3(x(1)+4:x(2)+3);
coeff13 = line_3(x(2)+4:x(3)+3);
coeff14 = line_3(x(3)+4:x(4)+3);
%converting string to number
a11 = str2num(coeff11);
a12 = str2num(coeff12);
a13 = str2num(coeff13);
a14 = str2num(coeff14);
%calling specific heat function along with passing the values
CP =specifi_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,temp_R,l_temp_med,R);
%calculating the entropy by calling function entropy
S =entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,temp_R,l_temp_med,R);
%calculationg enthalpy by calling function enthalpy
H =enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,temp_R,l_temp_med,R);
%calculating molecular weight
m_weight = molecular_wt(species_name);
%displying molecular weight of each species
sprintf("molecular weight of %s ",species_name)
disp(m_weight)
%making a new directory
mkdir(species_name);
cd(species_name);
%plot 1 enthalpy vs temperature
figure(1)
plot(temp_R,H,'LineWidth',3,'Color','r');
xlabel("temperature in K");
ylabel("enthalpy in KJ/mol")
title(sprintf("temperature vs enthalpy graph of %s",species_name));
%plot 2 entropy vs temperature
figure(2)
plot(temp_R,S,'LineWidth',3,'Color','r');
xlabel("temperature in K");
ylabel("entropy in KJ/mol-k")
title(sprintf("temperature vs entropy graph of %s",species_name));
%plot 3 specific heat vs temperature
figure(3)
plot(temp_R,CP,'LineWidth',3,'Color','r');
xlabel("temperature in K");
ylabel("specific heat in KJ")
title(sprintf("temperature vs specific heat of %s",species_name));
%saving the graph in the folder
saveas(figure(1),'enthalpy.jpg');
saveas(figure(2),'entropy.jpg');
saveas(figure(3),'specific_heat.jpg');
cd ..
end
AIM
Create a program that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data by reading this file NASA thermodynamic data
Calculate the molecular weight of each species and display it in the command window
Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.
Save the plots as images with appropriate names and dump them in separate folders for each species with suitable name and should be generated automatically
CONTENT
File parsing
To parse, where a string of commands – usually a program – is separated into more easily processed components, which are analyzed for correct syntax and then attached to tags that define each component.Reading in a data stream and building a memory model of the content of that data. This aims at facilitating perfoming some kind of transformation on the data. Parsing splits a file or other input into pieces of data that can be easily stored or manipulated.
NASA data contains polynomials that can be used to evaluate thermodynamic properties such as Cp, H, and S. The data contain the coefficients that are required to evaluate these polynomials for 1000+ species.
Solution Procedure: -
`(Cp)/R = a1+a2T+a3T^2+a4T^3+a5T^4`
`H/(RT) = a1+(a2*T)/2+(a3*T^2)/3+(a4*T^3)/4+(a5*T^4)/5+(a6)/T`
`S/R =a1*logT+a2*T+(a3*T^2)/2+(a4*T^3)/3+(a5*T^4)/4+a7`
Here R is the universal gas constant, T is the temperature
CP specific heat
H is the enthalpy
S is the entropy
Enthalpy
enthalpy, the sum of the internal energy and the product of the pressure and volume of a thermodynamic system. Enthalpy is an energy-like property or state function—it has the dimensions of energy (and is thus measured in units of joules or ergs), and its value is determined entirely by the temperature, pressure, and composition of the system and not by its history. In symbols, the enthalpy, H, equals the sum of the internal energy, E, and the product of the pressure, P, and volume, V, of the system: H = E + PV.
Entropy
entropy, the measure of a system's thermal energy per unit temperature that is unavailable for doing useful work. Because work is obtained from ordered molecular motion, the amount of entropy is also a measure of the molecular disorder, or randomness, of a system.
Specific heat
specific heat, the quantity of heat required to raise the temperature of one gram of a substance by one Celsius degree. The units of specific heat are usually calories or joules per gram per Celsius degree
Molecular weight
Molecular weight is a measure of the sum of the atomic weight values of the atoms in a molecule. Molecular weight is used in chemistry to determine stoichiometry in chemical reactions and equations. Molecular weight is commonly abbreviated by M.W. or MW.
The program is divided into different portions .The calculations such as entropy, enthalpy, specific heat, and molecular weight is done by calling the function.
Steps for the main program
The thermo.dat data is opened using the openfile code and saved in data variable
data =fopen('thermo.dat','r');
The first line is accessed using fgetl function.this is the heading of the data.
The 2nd line is accessed using fgetl. The temperature is split using strsplit command
temp = strsplit(temperature,' ');
The each temperature data in the form of cell array is converted into number using str2num command and stored in separate variable
Temperature range for plotting the graph is created using linespace command.
The constant value R is assigned to variable
The next three line is not required, for skipping the line 3 times fgetl command is used inside for loop innorder to execute three times
for i= 1:3
fgetl(data);
end
To access the data for loop is used
There is 53 set of molecules and each molecule have 14 coefficients to access.
So for loop runs 53 times to access 53 sets of data for calculation.
Inside the loop
The first line is acessed and stored in variable a
a =fgetl(data);
The data of line is split into strings using strsplit
line_data = strsplit(a,' ');
Reading the species name by accessing the first cell of cell array use { } because it is cell array
species_name = line_data{1};
Reading the local temperature of species stored by accessing the last three column in the end side .the temperature are converted to number from string using str2num command
l_temp_med = str2num(line_data{:,end-1});
l_temp_hig = str2num(line_data{:,end-2});
l_temp_low = str2num(line_data{:,end-3});
Extracting first line of the coefficient of species
line_1 = fgetl(data);
By finding the position of E and storing in the array ‘x’ we can store the coefficient .it is done by
findstr command in line_1
x = findstr(line_1,’E');
Extracting thr coeff using the position of E
First postion of E is x(1) and the coeff is from starting to the position of E +3
coeff1 = line_1(1:x(1)+3);
The 2nd coefficient starts x(1)+4 and ends at x(2)+3
coeff2 = line_1(x(1)+4:x(2)+3);
The 2nd coefficient starts x(2)+4 and ends at x(3)+3
coeff3 = line_1(x(2)+4:x(3)+3);
The 2nd coefficient starts x(3)+4 and ends at x(4)+3
coeff4 = line_1(x(3)+4:x(4)+3);
The 2nd coefficient starts x(4)+4 and ends at x(5)+3
coeff5 = line_1(x(4)+4:x(5)+3);
converting the coefficients from string to number
a1 = str2num(coeff1);
a2 = str2num(coeff2);
a3 = str2num(coeff3);
a4 = str2num(coeff4);
a5 = str2num(coeff5);
Get to the next line using fgetl
Similiarly accessing the second line and same step is done to get the data and stored in the variable. a6,a7,a8,a9,a10
Get the third line using get command
Similiarly getting the third line and same step is done to get the data and stored in the variable. a11,a12,13 and a14
Here the coefficient from a1 to a7 is coefficient for high_temperature and a8 to a14 low temperature coefficient
By calling specific heat function along with passing the values required for calculating the specific heat
CP =specifi_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,temp_R,l_temp_med,R);
Calculating the entropy by calling function entropy
S =entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,temp_R,l_temp_med,R);
Calculationg enthalpy by calling function enthalpy
H =enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,temp_R,l_temp_med,R);
Calculating molecular weight by calling the function
m_weight = molecular_wt(species_name);
displying molecular weight of each species using sprintf and disc command
sprintf("molecular weight of %s “,species_name)
disp(m_weight)
To store the graph plotted for each species and store in location seperately
Creating a folder with species name
mkdir(species_name);
Directing to the folder
cd(species_name);
plot 1 enthalpy vs temperature
Opening the a figure using figure command
figure(1)
Plotting temperature vs enthalpy
plot(temp_R,H,'LineWidth',3,'Color','r');
The x axis and y axis is labelled and the title is given
plot 2 entropy vs temperature
New figure window is created
figure(2)
plot(temp_R,S,'LineWidth',3,'Color','r');
The x axis and y axis is labelled and the title is given
plot 3 specific heat vs temperature
New figure window is created
figure(3)
plot(temp_R,CP,'LineWidth',3,'Color','r');
The x axis and y axis is labelled and the title is given
Each graph created in three figure window saved in the folder using saveas command
saveas(figure(1),'enthalpy.jpg');
saveas(figure(2),'entropy.jpg');
saveas(figure(3),'specific_heat.jpg');
To direct back to the folder use command cd(space)..
cd ..
This is the end of for loop the process is repeated 53 times to calculate the data for the 53 species and the graph are plotted and molecular weight is calculated for each species
Function for calculating specific heat
function [cpval] = specifi_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,temp_R,l_temp_med,R)
a1 to a7 high temperature coefficient & a8 to a14 low temp coefficient
Temp_R is the temperature range created using global temperature
l_temp_med is the medium temperature of species
R is the constant
if temp_R less the or equal to l_temp_med
Use the coefficient a8 to a14
By using the equation mentioned before
cpval= R*(a8+(a9.*temp_R)+ (a10.*temp_R.^2)+(a11.*temp_R.^3)+(a12.*temp_R.^4));
if temp_R greater than l_temp_med
cpval= R*(a1+(a2.*temp_R)+ (a3.*temp_R.^2)+(a4.*temp_R.^3)+(a5.*temp_R.^4));
The values are store in and passed to the junction specifc_heat
entropy function
function [sval] =entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,temp_R,l_temp_med,R);
a1 to a7 high temperature coefficient & a8 to a14 low temp coefficient
Temp_R is the temperature range created using global temperature
l_temp_med is the medium temperature of species
R is the constant
if (temp_R<=l_temp_med)
Use the coefficient a8 to a14
By using the equation mentioned before
sval = R.*((a8.*log(temp_R))+(a9.*(temp_R))+(a10.*((temp_R).^2)/2)+(a11.*((temp_R).^3)/3)+(a12.*((temp_R).^4))/4+a14;
else use the coefficient from a1 to a7
sval = R.*((a1.*log(temp_R))+(a2.*(temp_R))+(a3.*((temp_R).^2)/2)+(a4.*((temp_R).^3)/3)+(a5.*((temp_R).^4))/4+a7
enthalpy function
function [hval] = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,temp_R,l_temp_med,R);
a1 to a7 high temperature coefficient & a8 to a14 low temp coefficient
Temp_R is the temperature range created using global temperature
l_temp_med is the medium temperature of species
R is the constant
if (temp_R<=l_temp_med)
Use the coefficient a8 to a14
hval= R*temp_R.*(a8+(a9.*(temp_R)/2)+(a10*(temp_R).^2)/3+(a11*(temp_R).^3)/4+(a12*(temp_R).^4)/5+(a13/temp_R;
else use the coefficient from a1 to a7
hval= R*temp_R.*(a1+(a2.*(temp_R)/2)+(a3*(temp_R).^2)/3+(a4*(temp_R).^3)/4+(a5*(temp_R).^4)/5+a6/temp_R;
molecular weight function
function mw= molecular_wt(species_name)
The species name is stored in molecule
molecule = species_name;
storing the atoms required for calculation an array
atoms = ['C','H','O','N','A'];
storing corresponding atomic mass value in an array
mass = [12.011 1.0079 15.994 14.0067 39.948];
Assigning variable to zero
mw=0;
The I value execute length of molecule array
for i= 1:length(molecule)
J value execute 5 times for each I value
for j= 1:length(atoms)
For each character of molecule it is compared to all atoms using strcmpi command
if strcmpi(molecule(i),atoms(j));
If the strings are same the corresponding value of mass is added to mw
mw=mw+mass(j);
storing the position for checking the number of atoms
pos=j;
end of if statement
end of nested loop
calculating the number by converting string to number
k = str2num(molecule(i));
if the value od k>1 it is already executed one time,it has to add (k-1)times
if(k>1)
mw=mw+(mass(pos)*(k-1));
end of if statement
end of for loop
The molecular weight is calculated and passed through the function