A parsing the NASA thermodynamic data in MATLAB
AIM
To create a code for extracting the all coefficients of an individual species and introduce to calculation for calculating the specific heat, entropy and enthalpy. Additionally, to calculate the molecular weight for each species.
OBJECTIVES
1) Write a program for extracting the 14 coefficients in each species in the given data file and that involves into our calculations.
2) Create a looping for calculating the specific heat, enthalpy and entropy all are with function of temperature.
3) Create a function for calculating the molecular weight in each species.
4) Create a program for saving the plots in automatically with individual folders for each species.
INTRODUCTION
In this project we extracting the datas from the given NASA thermodynamic data by using file parsing method in matlab. By apply a basic logic and basic code to extracting the 14 coefficients in each species in proper manner. We are doing a program for parse out in the given data.
Afterthat, extracted values are involved into our calculation and make a figure for each species with individual properties such as specific heat, entropy and enthalpy. Additionally, calculating the molecular weights for individual species. We create a code for automatically saving the figure with individual folder.
File parsing
The file parsing is defines, how the information has been reading or writing by using computer language and that information used in our calculation. If we had know the file parsing, we have do parse in or parse out in text file or any type of file.
Polynomial
A polynomial is an expression consisting of variables (also called indeterminates) and coefficients, that involves only the operations of addition, subtraction, multiplication, and non-negative integer exponents of variables.
Coefficient
A coefficient is a multiplicative factor in some term of a polynomial, a series, or any expression; it is usually a number, but may be any expression.
Thermodynamic properties
Specific heat
The specific heat capacity of a substance is the amount of energy required to raise the temperature of 1kg of the substance by `1^oC`. It denoted by Cp, SI units are J/kg.K
Enthalpy
Enthalpy, a property of a thermodynamic system, is the sum of the system's internal energy and the product of its pressure and volume. It denoted by H, SI units are J/mol.
Entropy
Entropy can be defined in a simple qualitative way as a measure of the degree of randomness of the particles, such as molecules in a system. It denoted by S, SI units are J/K
FORMULA FOR FINDING THE SPECIFIC HEAT, ENTHALPY AND ENTROPY
`(Cp)/R = a1+a2+a3T^2+a4T^3 +aT^4`
`H/(RT) = a1+a2(T/2) + a3(T^2/3)+a4(T^3/4) +a5(T^4/5)+(a6)/T`
`S/R = a1 lnT+a2(T) +a3(T^2/2)+a4(T^3/3)+a5(T^4/4)+a7`
Where,
a1 to a14 = Polynomial variables
T = Temperature in K
Cp = Specific heat
H = Enthalpy
S = Entropy
R = Universal gas constant
Finding universal gas constant value
Unit conversation
`PV = muRT`
`R = (PV)/(muT)`
`R = (Nxxm^3)/(m^2xxKxx1)`
`R = (Nxxm)/(Kxxmol)`
`R = J/(molxxK)`
`R = Jxxmol^-1xxK^-1`
For substitution the values
`T = 0^o C = 273 K`
`P = 1.01xx10^5 (N/m^2)`
`V = 22.4 lit = 22.4xx10^-3 m^3` From the avogadro hypothesis
`R = (1.01xx10^5xx22.4xx10^-3)/(1xx273)`
`R = 8.31 J*mol^-1*K^-1`
MAIN PROGRAMMING FOR PARSE OUT THE DATA
clear all % Remove the all variables and corresponding values from the workspace tab
close all % Closing the all figures
clc % Clean the all data from command window
% check the location of current folder % Make it in save in apropriate folder
Folder_location = pwd; % pwd command shows the current folder
% Import the data file for parse out
f1 = fopen('THERMO.dat','r'); % f1 is the file handle, it actually a pointed to the file.
% fopen command open the given imported file in this code
% Import the appropriate file, make sure that file had saved in same folder.
% Reading header information from thermo.dat data file
Title = fgetl(f1); % fgetl command is read the data in line by line
% Reading the global temperature range
Temp_range = fgetl(f1); % Parse out the global temperature by using the fgetl command
A = strsplit(Temp_range,' '); % Split the global temperature string by using the strsplit command
% Convert string to number % Convert the string to numbers by using str2num command
Global_low_temperature = str2num(A{2});
Global_mid_temperature = str2num(A{3});
Global_high_temperature = str2num(A{4});
% Skipping the three comment lines % Ignore command lines
Exclamation_1 = fgetl(f1);
Exclamation_2 = fgetl(f1);
Exclamation_3 = fgetl(f1); % Matlab can read upto this but we ignored in our calculation
%% Reading the coefficients for individual species
% Reading species name
for i = 1:53; % For loop will store the each species coefficients and local temp at each iteration
Line_1 = fgetl(f1); % Parse out the local temperature range
Line_1str = strsplit(Line_1,' '); % Split the Line_1
Species_name = (Line_1str{1}) % Parse out the species name
Local_temp = strsplit(Line_1,' '); % Parse out the local temperatures
Local_low_temperature = str2num(Local_temp{6}); % Convert the string to number at local low temperature
Local_high_temperature = str2num(Local_temp{7}); % Convert the string to number at local medium temperature
Local_mid_temperature = str2num(Local_temp{8}); % Convert the string to number at local high temperature
Temp = linspace(Local_low_temperature,Local_high_temperature,100); % Divide the local temperature in linearly spaced vectors by using linspace command
R = 8.314; % R is the universal gas constant from the avogadro's hypothesis
% Extracting the coefficients % Parse out the coefficients for calculating in our polynomials
Line_2 = fgetl(f1);
Line_3 = fgetl(f1);
Line_4 = fgetl(f1);
% Identifying the exponential variable 'E'
a = findstr(Line_2,'E'); % Finding the exponential variable in each coefficients string at line_2 by using findstr command
b = findstr(Line_3,'E'); % Finding the exponential variable in each coefficients string at line_3 by using findstr command
c = findstr(Line_4,'E'); % Finding the exponential variable in each coefficients string at line_4 by using findstr command
% Convert the each and every coefficients into the number
% First seven coefficients are low temperature coefficients
a1 = str2num(Line_2(1:a(1)+3));
a2 = str2num(Line_2(a(1)+4:a(2)+3));
a3 = str2num(Line_2(a(2)+4:a(3)+3));
a4 = str2num(Line_2(a(3)+4:a(4)+3));
a5 = str2num(Line_2(a(4)+4:a(5)+3));
a6 = str2num(Line_3(1:b(1)+3));
a7 = str2num(Line_3(b(1)+4:b(2)+3));
% Last seven coefficients are high temperature coefficeients
a8 = str2num(Line_3(b(2)+4:b(3)+3));
a9 = str2num(Line_3(b(3)+4:b(4)+3));
a10 = str2num(Line_3(b(4)+4:b(5)+3));
a11 = str2num(Line_4(1:c(1)+3));
a12 = str2num(Line_4(c(1)+4:c(2)+3));
a13 = str2num(Line_4(c(2)+4:c(3)+3));
a14 = str2num(Line_4(c(3)+4:c(4)+3));
% Making a folder for saving the plots for each and every species
mkdir(['D:\MATLAB challange and projects\Project 1 Parsing NASA thermodynamics
data\',Species_name]); % mkdir command means making a directory
% Calculating the thermodynamic system properties
for j = 1:length(Temp); % Looping till last temperature values
if Temp(j) < Local_mid_temperature; % If condition is decision making command
Specific_heat = (a1 + a2.*Temp + a3.*Temp.^2 + a4.*Temp.^3 + a5.*Temp.^4).*R;
Enthalpy = (a1 + (a2.*Temp)/2 + (a3.*Temp.^2)/3 + (a4.*Temp.^3)/4 + (a5.*Temp.^4)/5 + a6./Temp).*(R.*Temp);
Entropy = (a1.*log(Temp) + a2.*Temp + (a3.*Temp.^2)/2 + (a4.*Temp.^3)/3 + (a5.*Temp.^4)/4 + a7).*R;
else % Temperature crossing above the mid point, the discussion takes place
Specific_heat = (a8 + a9.*Temp + a10.*Temp.^2 + a11.*Temp.^3 + a12.*Temp.^4).*R;
Enthalpy = (a8 + (a9.*Temp)/2 + (a10.*Temp.^2)/3 + (a11.*Temp.^3)/4 + (a12.*Temp.^4)/5 + a13./Temp).*(R.*Temp);
Entropy = (a8.*log(Temp) + a9.*Temp + (a10.*Temp.^2)/2 + (a11.*Temp.^3)/3 + (a12.*Temp.^4)/4 + a13).*R;
end % End the if condition
end % End the for loop condition
% Plotting Temperature vs Specific heat
figure(1)
plot(Temp,Specific_heat ,'linewidth',3,'color','r')
title(Species_name)
xlabel('Temperature in [K]')
ylabel('Specific heat in [J.Kmol^-1.K^-1]')
legend('Specific heat with function of temperature')
% Temperature vs Enthalpy
figure(2)
plot(Temp,Enthalpy ,'linewidth',3,'color','r')
title(Species_name)
xlabel('Temperature in [K]')
ylabel('Enthalpy in [J.mol^-1]')
legend('Enthalpy with function of temperature')
% Temperature vs Entropy
figure(3)
plot(Temp,Entropy,'linewidth',3,'color','r')
title(Species_name)
xlabel('Temperature in [K]')
ylabel('Entropy in [J.K^-1]')
legend('Entropy with function of temperature')
directory = (['D:\MATLAB challange and projects\Project 1 Parsing NASA thermodynamics data\',Species_name]); % Species folder is created
cd(directory); % Open the species folder and get ready to saving
% Saving the plots % Saving the figures in inside the species folder
saveas(1,'Specific heat.jpg')
saveas(2,'Enthalpy.jpg')
saveas(3,'Entropy.jpg')
cd('D:\MATLAB challange and projects\Project 1 Parsing NASA thermodynamics data\'); % One step back from the current folder
% Calculating the molecular mass in each species individually
Molecular_weight = molecular_mass(Species_name) % Function operates to calculate the molecular mass for species
end % End the for loop condition for calculating the group of coefficients
fclose(f1); % Close the file handle
ILLUSTRATION
1) A f1 is a file handle, it actually a pointed to the file and type it the fopen command it stands for open the appropriate imported file in additionally, we mentioning the file name with permission to writing mode.
2) First of all, we parse out the header information in the given data file, by using the fgetl command. In the header information, we mainly parse out the file title and global temperature range of the whole system.
3) Split the string line by using strsplit command and make to convert the string to number by using str2num command. We don't consider the global temperature into calculation.
4) Ignore the exclamation command line into the our calculations.
5) We parse out the each species with all coefficients in line by line by using the commands such as fgetl, findstr, strsplit, str2num and so on in iteration process till the end.
6) Divide into linearly spaced vector from local low temperature to local high temperature in automatically matab can do and also we calculate the universal gas constant value from the avogadro's hypothesis
7) Making a directory for saving the results in individually.
8) Create a looping for finding the thermodynamic property variation with function of temperature.
9) We coded for calculating the thermodynamic properties like specific heat, enthalpy and entropy. We know the first seven coefficients are low temperature and last seven coefficients are high temperature.
10) By using the if condition, it is decision making comment. When the temperature is less than the local middle temperature it will passes to first sorted equations like Cp,H and S. If suppose temperature is crossing the local middle temperature it passing to else condition and end it the if condition with for loop in subsequently.
11) Make a figure and save the plots in automatically with appropriated naming folders.
12) Additionally, we want to calculate the molecular weight in each and every species. So, we create a function, that function is connection between to main coding.
13) End it the for loop and close the file pointer with fclose command.
FUNCTION PROGRAM FOR CALCULATING MOLECULAR WEIGHT
function MW = molecular_mass(Species_name); % Function for calculating the molecular mass
% Passing the species name when its looping in main code
Species = upper(Species_name); % Upper command converts all should capital
% Input data for atoms and atomic masses
Atom = ['H','C','N','O','A']; % Atom annotations
Atomic_mass = [1.00794 12.0107 14.0067 15.9994 39.948]; % Corresponding atomic mass for atoms
for i = 1:length(Species);
for j = 1:length(Atom);
if strcmp(Species(i),Atom(j)); % strcmp command compare the atoms and excute to function
MM = Atomic_mass(j);
u = j;
end
end
n = str2num(Species(i)); % Some of the cases atoms exist more than 1, the sorted if condition is operates
if n > 1;
MW = MM + Atomic_mass(u)*(n-1); % Add it the molecular mass and times of atomic mass
end
end
end
ILLLUSTRATION FOR FUNCTION CODE
1) In this function mainly for finding the molecular weight for each species.
2) The atoms had been consist of subatomic particles like protons, electrons, neutrons. In each atom has some calibrated atomic masses
3) When two atoms are mixed, that is called molecule, in each molecule has some calibrated molecular weight.
4) Something, we doing for calculating the molecular weight in matlab.
5) We collect the atoms annotations and corresponding atomic masses in the array. The index had placed in correct location, then string compare command will works.
6) Strcmp command it automatically compared with species and atom over the length of iterations, when the final value gets 1 that means both of us in same.
7) If some of the species have more than one in same atom, that times else condition will operates the equations.
8) Finally, it passing the molecular weight for each species and it displayed into command window.
CODING COMMANDS
fgetl - Using this command we parse out the data in line by line.
fopen - Open the appropriate import file at current with reading permission.
Strsplit - This command help to split the string in the line and display it how many cell have in that particular line.
Str2num - In each cell has not number, by using this command, that element of cell is converts into the number.
linspace - Divide into linearly spaced vector from initial value to final value.
findstr - By using this command we get identified the location of that variable or number. Find the coefficients by referance that variable.
fprintf - This command help us to printing the appropriated data file or any other file.
for loop - for loop has stored only one value and excute to the equation inside the looping condition
If statement - This condition making a decision for what do.
RESULTS
1) DIOXYGEN GAS
ENTHALPY VARING WITH FUNCTION OF TEMPERATURE
ENTROPY VARYING WITH FUNCTION OF TEMPERATURE
SPECIFIC HEAT VARYING WITH FUNCTION OF TEMPERATURE
NITROGEN GAS
ENTHALPY VARING WITH FUNCTION OF TEMPERATURE
ENTROPY VARYING WITH FUNCTION OF TEMPERATURE
SPECIFIC HEAT VARYING WITH FUNCTION OF TEMPERATURE
3) CARBON DIOXIDE GAS
ENTHALPY VARYING WITH FUNCTION OF TEMPERATURE
ENTROPY VARYING WITH FUNCTION OF TEMPERATURE
SPECIFIC HEAT VARYING WITH FUNCTION OF TEMPERATURE
MOLECULAR WEIGHT FOR EACH SPECIES
WORKSPACE
SCREENSHOTS FOR SHOWING SPECIES FOLDERS
ERRORS FACED WHILE DOING IN PROGRAMMING
In this error occuring reason is I am using the normal multiplication instead of elementwise multiplication. Afterthat, I can rewrite it runs smoothly.
Samething, I missed again for elementwise operator of division instead of using normal division. Afterthat, I recorrect and execute the code.
I text it wrong folder information when a making a directory. Afterthat, I change into Species_name it works properly.
CONCLUSION
Finally, make a graphs for individual species with polynomial eqaution to saved it in automatically in the individual folder in appropriately.
Displaying the molecular weights in the command window.
REFERANCES
1) https://in.mathworks.com/help/matlab/ref/strcmp.html
2) https://in.mathworks.com/help/matlab/ref/if.html
3) https://en.wikipedia.org/wiki/Gas_constant