Project 1 - Parsing NASA thermodynamic data
Aim:
To write a code in MATLAB to parse the NASA thermodynamic data file and calculate the thermodynamic properties of various gas species.
Objective:
- To write a function that extracts the 14 co-efficients and calculates the specific heat, enthalpy and entropy for all the species in the data file.
- To calculate the molecular weight of each species and display it in the command window.
- To plot 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 suitable name and all these should be done automatically.
- The main program will just take the 'THERMO.dat' file as input and generate Cp, H and S plots.
Theory:
File Parsing:
It is the method of splitting the file or other input into a piece of data that can be easily stored or manipulated. By doing this we are going to split a string into parts then recognizing the parts to convert it into something simpler than a string.
Here in this project we are going to split the NASA thermodynamic data which is having 53 gas species values in 213 lines. To perform the calculation for thermodynamic properties, we need some sort of co-efficient values and temperature ranges which is available in the NASA file. So, we are going to parse the values which are all needed for our calculation from that file.
Specific Heat:
Specific heat is the amount of heat energy required to raise the temperature of body per unit of mass. Specific heat is also known as specific heat capacity or mass specific heat. In SI units, specific heat is the amount of heat in joules required to raise 1 gram of a substance 1 Kelvin.
Enthalpy:
When a substance changes at constant pressure, enthalpy tells how much heat and work was added or removed from the substance. Enthalpy is similar to energy but not the same. When a substance grows or shrinks, energy is used up or released.
Entropy:
The entropy of an object is a measure of the amount of energy which is unavailable to do work. Entropy is also a measure of the number of possible arrangements of atoms a system can have. In this sense, entropy is the measure of uncertainty or randomness.
Gas Constant:
The gas constant is a physical constant denoted by R and is expressed in terms of units of energy per temperature increment per mole. The gas constant value is equivalent to Boltzmann constant but expressed as the pressure-volume product instead of nergy per increment of temperature per particle.
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.
Governing equationsa:
Cp/R = a1+a2.T+a3.T^2+a4.T^3+a5.T^4
H/RT = a1+a2.T/2+a3.T^2/2+a4.T^3/4+a5.T^4/5+a6/T
S/R=a1.lnT+a2.T+a3.T2^2/2+a4.T3^3/3+a5.T4^4/4+a7
where,
Cp = Specific Heat (J/K kg)
H = Enthalpy (J)
S = Entropy (J/K)
R = Universal gas constant = 8.314 (J/mol.K)
T = Temperature (K
Main program
% parsing NASA Thermodynamics data
clear all
close all
clc
% getting the data from provided file
f1 = fopen('THERMO.dat','r');
fgetl(f1);
tline = fgetl(f1);
A = strsplit(tline,' ');
Global_low_temp = str2num(A{2});
Global_mid_temp = str2num(A{3});
Global_high_temp = str2num(A{4});
% Eliminating the Lines
for i = 3:5
b = fgetl(f1);
end
% Getting the Temperature & Coefficients for each Species
for j = 1:53
species = fgetl(f1);
t = findstr(species,'.');
% Local Temperature for each species
species(52-3:52+3);
Tl = species(52-3:t(1)+3);
Tl1 =str2num(Tl);
species(t(1)+6:62+3);
Th = species(t(1)+6:t(2)+3);
Th1 =str2num(Th);
species(t(2)+6:72+3);
Tm = species(t(2)+6:t(3)+3);
Tm1 = str2num(Tm);
sp_name = species(1:6);
% Coefficients of each species
coeff_line_1 = fgetl(f1);
A11 = strfind(coeff_line_1,'E');
coeff_line_1(1:12+3);
a1 = coeff_line_1(1:A11(1)+3);
c1 = str2num(a1);
a2 = coeff_line_1(A11(1)+4:A11(2)+3);
c2 = str2num(a2);
a3 = coeff_line_1(A11(2)+4:A11(3)+3);
c3 = str2num(a3);
a4 = coeff_line_1(A11(3)+4:A11(4)+3);
c4 = str2num(a4);
a5 = coeff_line_1(A11(4)+4:A11(5)+3);
c5 = str2num(a5);
coeff_line_2 = fgetl(f1);
A12 = strfind(coeff_line_2,'E');
coeff_line_2(1:12+3);
a6 = coeff_line_2(1:A12(1)+3);
c6 = str2num(a6);
a7 = coeff_line_2(A12(1)+4:A12(2)+3);
c7 = str2num(a7);
a8 = coeff_line_2(A12(2)+4:A12(3)+3);
c8 = str2num(a8);
a9 = coeff_line_2(A12(3)+4:A12(4)+3);
c9 = str2num(a9);
a10 = coeff_line_2(A12(4)+4:A12(5)+3);
c10 = str2num(a10);
coeff_line_3 = fgetl(f1);
A13 = strfind(coeff_line_3,'E');
coeff_line_3(1:12+3);
a11 = coeff_line_3(1:A13(1)+3);
c11 = str2num(a11);
a12 = coeff_line_3(A13(1)+4:A13(2)+3);
c12 = str2num(a12);
a13 = coeff_line_3(A13(2)+4:A13(3)+3);
c13 = str2num(a13);
a14 = coeff_line_3(A13(3)+4:A13(4)+3);
c14 = str2num(a14);
% calculating the specific Heat,Enthalphy,Entropy
T = linspace(Tl1,Th1,100);
R = 8.314;
[Cp,H,S] = filepar(T,R,Tm1,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14)
%plotting
figure(1)
plot(T,Cp,'Linewidth',3,'color','b');
xlabel('Temp');
ylabel('Specific Heat');
title(sp_name);
figure(2)
plot(T,H,'LineWidth',3,'Color','b');
xlabel('Temp');
ylabel('Entropy');
title(sp_name);
figure(3)
plot(T,S,'LineWidth',3,'Color','b');
xlabel('Temp');
ylabel('Enthalpy')
title(sp_name);
% creating the folder
mkdir(sp_name)
% saving the plots
cd(sp_name)
saveas(1,'Specific Heat.jpg')
saveas(2,'Entropy.jpg')
saveas(3,'Enthalphy.jpg')
cd ..
% calculating the molecular weight for each species
mw = molecular_weight(sp_name)
end
fclose(f1)
ode explanation for main program
Initially we will opening the given THERMO.dat file using fopen command and then we will extract all the information provided in the file using fgetl command.
-First we we will get the global temperature by using strslplit command and then we skip some lines using the fget command.
-Now we make use of for loop command to get all temperature and coefficients that are presnt in our file.
-For getting temperature and coefficients we use finstr command inorder to input the accurate spot and specifying it so that we can get all the data.
-Now the datas which are obtanied from the file will be of string format which we need to convert into numbers by using str2num command.and by this we will get the temperature and coefficient of all the species.
-Now the specific heat,enthalpy ,entropy function is calculated and gets plotted.
-Now the mkdir command is used by specifying the directory.
-The three figures are plotted using plot command and each species name is displayed using title command.And further it will named using xlabel and ylabel command.
-Now we are changing the dierectory using cd command and then we will save the plots as our images using save command.
-Finally we will again change the dierectory using cd command to calculate molecular weight of each species using the function program and closing the file using fclose command.
Function program
% Specific heat, Enthalpy & Entropy Calculation
function [Cp,H,S] = inputs(T,R,Tm1,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14)
for i = 1:length(T)
if T(i)>Tm1
Cp= (c1 + c2.*T + c3.*T.^2 + c4.*T.^3 +c5.*T.^4).*R;
H = (c1 + (c2.*T)./2 + (c3.*T.^2)./3 + (c4.*T.^3)./4 +(c5.*T.^4)./5 + c6./T).*T.*R;
S = (c1.*log(T) + c2.*T + (c3.*T.^2)./2 + (c4.*T.^3)./3 +(c5.*T.^4)./4 + c7).*R;
else
Cp = (c8 + c9.*T + c10.*T.^2 + c11.*T.^3 +c12.*T.^4).*R;
H = (c8 + (c9.*T)./2 + (c10.*T.^2)./3 + (c11.*T.^3)./4 +(c12.*T.^4)./5 + c13./T).*T*R;
S = (c8.*log(T) + c9.*T + (c10.*T.^2)./2 + (c11.*T.^3)./3 +(c12.*T.^4)./4 + c14).*R;
end
end
end
Code explained:
-By using the funtion program we can calculate the specific heat,enthalpy,entropy will be calculated and here we make use of for loop to calculate for each species.
-Also here we make use of if and else command to calculate high tempurature for the first 7 elements and low temperature for last 7 elements.
Function program:
% Molecular Weight Calculation
function molecular_weight_calculation = molecular_weight(sp_name)
species = (sp_name)
elements = ['H','C','O','N','A','S'];
atomic_weight = [1.00794 12.0107 15.999 14.0067 39.948 32.065];
molecular_weight_calculation = 0;
for i = 1:length(species)
for j = 1:length(elements)
if strcmp(species(i),elements(j))
molecular_weight_calculation = molecular_weight_calculation + atomic_weight(j);
a = j;
end
end
n = str2num(species(i));
if n>gt;1
molecular_weight_calculation = molecular_weight_calculation + atomic_weight(a)*(n-1);
end
end
end
Code explained:
-By using the above funtion code we can calculate the molecular weight of all the species.Here we give atomic weight and atomic name as input to the species.
-Then we make use of for loop to calculate the molecular weight of each species individually.Then further we use command called strcmp to compare the species with the elements and calculate their molecular weight.
-And the string is converted to number by using str2num command.
Error faced while programming:
the error faced was warning directory already exist this error was rectified using if ~exist command
another error faced was i was un aware of closing the for loop by using 'end ' at last..
Results:
The code to prase the NASA thermodynamic data file has been programmed and the thermodynamic properties and molecular weight of species has been calculated and plotted.