Project 1 - Parsing NASA thermodynamic data using Matlab
Aim-
- Write a program that extracts the 14 coefficients from given THERMO.DAT file
- 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 (specific for each species).
- To save the plots as images with appropriate names and dump them in separate folders for each species.
Theory-
PARSER:A parser is a compiler or interpreter component that breaks data into smaller elements for easy translation into another language. A parser takes input in the form of a sequence of tokens or program instructions and usually builds a data structure in the form of a parse tree or an abstract syntax tree.
FORMULA USED:
H-Enthalpy
S-Entropy
Cp-Specific heat
R-Universal gas constant(8.314 J/mol.K)
T-Temperature
Enthalpy: It is equal to the internal energy of the system plus the product of pressure and volume.
H = (a1 + a2 T /2 + a3 T^2 /3 + a4 T^3 /4 + a5 T^4 /5 + a6/T)*R*T
Entropy: A thermodynamic quantity representing the unavailability of a system's thermal energy for conversion into mechanical work, often interpreted as the degree of disorder or randomness in the system.
S = (a1 lnT + a2 T + a3 T^2 /2 + a4 T^3 /3 + a5 T^4 /4 + a7)*R
Specific heat: The heat in calories required to raise the temperature of one gram of a substance one degree Celsius.
Cp = (a1 + a2 T + a3 T^2 + a4 T^3 + a5 T^4)*R
Molecular weight: The average mass of a molecule of a compound compared to ¹/₁₂ the mass of carbon 12 and calculated as the sum of the atomic weights of the constituent atoms.
MATLAB CODE & EXPLANATION
1) Function for Molecular Weight calculation-
function [molecular_weight] = mole_wt(species_name)
SPECIES = upper(species_name);
constituents = {'H','C','N','O','A'}; % Declaring species elements
%Atomic weight of each elements
atomic_weight = [1 12 14 16 40];
molecular_weight = 0; % Declaring molecular weight is '0' in initial stage
for i = 1:length(SPECIES)
for j = 1:length(constituents)
if strcmp(SPECIES(i),constituents(j))
molecular_weight = molecular_weight + atomic_weight(j);
%Storing the position of matched element
u = j;
end
end
n = str2double(SPECIES(i));
if n > 1
molecular_weight = molecular_weight + atomic_weight(u)*(n-1);
end
end
- A string array is created which contains the name of the atoms.
- A variable atomic_weight is created which contains the atomic weight of respective atoms in the string array.
- Then used strcmp command to compare the name of the element with the string array.
- If there is repeatition of any element, str2num is used to find that and (n-1) is multiplied to atomic_weight of element to calculate molecular weight.
2) Thermodynamics properties function that calculates Specific heat, Enthapy and Entropy-
% Specific-heat, Enthapy and Entropy
function [cp, h, s] = thermodynamic_properties(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,R,T,global_low_temp,global_mid_temp,global_high_temp)
for j = 1:length(T)
if T(j) <= global_mid_temp
% Relations used if T(j) <= global_mid_temp
cp = (a1 + a2.*T + a3.*T.^2 + a4.*T.^3 + a5.*T.^4).*R;
h = (a1 + (a2.*T)./2 + (a3.*T.^2)./3 + (a4.*T.^3)./4 + (a5.*T.^4)./5 + (a6./T)).*R.*T;
s = (a1.*log(T) + a2.*T + (a3.*T.^2)./2 + (a4.*T.^3)./3 + (a5.*T.^4)./4 + a7).*R;
% Relations used if T(j) > global_mid_temp
else
cp = (a8 + a9.*T + a10.*T.^2 + a11.*T.^3 + a12.*T.^4).*R;
h = (a8 + (a9.*T)./2 + (a10.*T.^2)./3 + (a11.*T.^3)./4 + (a12.*T.^4)./5 + (a13./T)).*R.*T;
s = (a8.*log(T) + a9.*T + (a10.*T.^2)./2 + (a11.*T.^3)./3 + (a12.*T.^4)./4 + a14).*R;
end
end
end
This function uses the high temperature coefficeints, low temperature coefficients, universal gas constant, global minimum, global medium and global maximum temperature values as the input parameters. Then we use the if else conditional statement to check if the temp values are less than or equal to the global medium temperature values and select the appropriate equations (defined for all the parameters to calculate) with high and low temperature coefficients is used to calculate the thermodynamic properties like specific heat(cp), enthalpy(h) and entropy(s).
3) Function to write molecular weight in a text file-
function writing_to_file(species_name,weight)
dir_name = species_name;
mol_file = fopen('mol_wt.txt' , 'a+'); % Mention fime name & its type
fprintf (mol_file, 'Molecular weight of %s is %f \n', dir_name,weight); % Printing species name & molecular weight in file
fclose (mol_file);
end
- Firstly a new file is created with name 'mol_wt.txt' and it is opened in append mode denoted by 'a+' so that we can write the molecular weight of all elements in a single file.
- Using the fprintf command the name of the element along with their molecular weight is written to the file.
- The file is then closed.
4) Main code
% Program for
% 1. Write a program that extracts the 14 coefficients from given THERMO.DAT file
% 2. Calculate the molecular weight of each species and display it in the command window.
% 3. Plot the Cp, Enthalpy and Entropy for the local temperature range (specific for each species).
% 4. To save the plots as images with appropriate names and dump them in separate folders for each species.
clear all
close all
clc
% Inputs
R = 8.314; % Value of Gas constant
% Reading the thermodynamic data file
f1 = fopen('THERMO.dat','r'); % opening the file for reading
fgetl(f1); % Reading file Header
t_line = fgetl(f1); % Reading second line
A = strsplit(t_line,' '); % Splitting string from the line
% Defining the temperatures extracted from data file
global_low_temp = str2double(A{2}); %Global Low Temperature
global_mid_temp = str2double(A{3}); %Global Medium Temperature
global_high_temp = str2double(A{4}); %Global High Temperature
%Ignoring the 3 line as a comment in the file
for i = (1:3)
comments = fgetl(f1);
end
% Using for loop to consider 53 elements
for i = 1:53
% Extracting species name
species = fgetl(f1); %Reading the first line which includes the information for species
species_name = species(1:6); %Reading the name of the species
% Extracting coefficients from first line
line_1 = fgetl(f1); %Reading the first line which contains 7 high temp & 7 low temp coefficients
l1 = strfind(line_1,'E'); %Finding the position of E so that we can extract the coefficients
a1 = str2double(line_1(1:(l1(1)+3))); % Storing 1st coefficient
a2 = str2double(line_1((l1(1)+4):(l1(2)+3))); % Storing 2nd coefficient
a3 = str2double(line_1((l1(2)+4):(l1(3)+3))); % Storing 3rd coefficient
a4 = str2double(line_1((l1(3)+4):(l1(4)+3))); % Storing 4th coefficient
a5 = str2double(line_1((l1(4)+4):(l1(5)+3))); % Storing 5th coefficient
% Extracting coefficients from second line
line_2 = fgetl(f1); %Reading the first line which contains 7 high temp & 7 low temp coefficients
l2 = strfind(line_2,'E'); %Finding the position of E so that we can extract the coefficients
a6 = str2double(line_2(1:(l2(1)+3))); % Storing 6th coefficient
a7 = str2double(line_2((l2(1)+4):(l2(2)+3))); % Storing 7th coefficient
a8 = str2double(line_2((l2(2)+4):(l2(3)+3))); % Storing 8th coefficient
a9 = str2double(line_2((l2(3)+4):(l2(4)+3))); % Storing 9th coefficient
a10 = str2double(line_2((l2(4)+4):(l2(5)+3))); % Storing 10th coefficient
% Extracting coefficients from third line
line_3 = fgetl(f1); %Reading the first line which contains 7 high temp & 7 low temp coefficients
l3 = strfind(line_3,'E'); %Finding the position of E so that we can extract the coefficients
a11 = str2double(line_3(1:(l3(1)+3))); % Storing 11th coefficient
a12 = str2double(line_3((l3(1)+4):(l3(2)+3))); % Storing 12th coefficient
a13 = str2double(line_3((l3(2)+4):(l3(3)+3))); % Storing 13th coefficient
a14 = str2double(line_3((l3(3)+4):(l3(4)+3))); % Storing 14th coefficient
% Creating a folder to save thermodynamic plots of the species
mkdir(['C:\Users\HP\Desktop\matlab assignment\PARSING THE DATA\plots_of_', species_name]);
T = linspace(global_low_temp, global_high_temp, 500); %Ranging the temperature low temperature to high temperature
% Calling the Thermodynamic properties function
[cp, h, s] = thermodynamic_properties(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,R,T,global_low_temp,global_mid_temp,global_high_temp);
% Plotting the different plots for the species
% Plot between Temperature and Specific Heat
figure(1)
plot(T,cp,'linewidth',3,'color','b')
title ({'Specific Heat Vs Temperature',['Species name = ',species_name]})
xlabel('TEMPERATURE (K)')
ylabel('SPECIFIC HEAT (J/kg-K)')
% Plot between Temperature and Enthalpy
figure(2)
plot(T,h,'linewidth',3,'color','g')
title ({'Enthalpy Vs Temperature',['Species name = ',species_name]})
xlabel('TEMPERATURE (K)')
ylabel('ENTHALPY (J/kg)')
% Plot between Temperature and Entropy
figure(3)
plot(T,s,'linewidth',3,'color','c')
title ({'Entropy Vs Temperature',['Species name = ',species_name]})
xlabel('TEMPERATURE (K)')
ylabel('ENTROPY (J/kg-K)')
% Saving the plotted file in the system directory with the folder name
directory = (['C:\Users\HP\Desktop\matlab assignment\PARSING THE DATA\plots_of_',species_name]);
cd(directory)
% Saving plots
saveas(1,'specific_heat.png')
saveas(2,'enthalpy.png')
saveas(3,'entropy.png')
% Changing directory for calculating molecular weight
cd('C:\Users\HP\Desktop\matlab assignment\PARSING THE DATA')
molecular_weight = mole_wt(species_name);
% Printing molecular weight in command window
fprintf(species_name)
fprintf('molecular_weight of %s:- ',species_name)
fprintf('%d',molecular_weight)
fprintf(' ')
fprintf('amu')
fprintf('\n')
%Function to write molecular weight to a file
weight = mole_wt(species_name);
writing_to_file(species_name,weight)
end
fclose(f1);
- Firstly in the program the value of the universal gas constant R is given as 8.314 as an input.
- Then using the fopen command which is used to open the THERMO.dat file in the read mode specified by 'r' keyword in the fopen command.
- The file is read line by line by using the fgetl command. The first line represents the title if the file so it is ignored.
- Since the temperatures are inside the string we use 'strsplit' command that splits the string in the specified delimiter, here ' ' (space) is the delimiter, after splitting the values are assigned to variables ( global low, mid and high temperatures), using 'str2double' command string value is converted to the numeric value.
- The next line in the file represents global temperatures, thus the values are stored for temperature calculations.
- The next three lines are comments in the file so they are ignored by using a for loop to iterate for 3 values.
- Then a for loop is created to loop through the 53 species data given in the file and clculate the required values.
- Inside the loop the fgetl command is used to read the next line of the file which represents the name of the species.
- The name of the element is stored in a variable using fget1 command.
- Then the next line of the file is read which contains the high and low temperature data of the particular species.
- The read lines are in the form of a string so they are converted into numbers using the strdouble command and stored in specific variables so they can be used for further calculations.
- The low temperatures values are stored in variables a1,a2,a2,a4,a5,a6 and a7 and the high temperature values are stored invariables a8,a9,a10,a11,a12,a13 and a14.
- Than creating a directory to save the plots of different species.
- Then the temperature array is defined using linspace command as 500 values between the global minimum and global maximmum values.
- The next line is the reference to a function called thermodynamic properties which calculates and returns the value of specific heat, enthalpy and entropy using the high and low temperature values as inputs.
- The next line is to plots & graph(T-S, T-H & T-Cp) for different species using the given inputs by giving them title, xlabel, ylabel and various plot properties and saving them and mentioning the directory.
- Now changing directory for calculating molecular weight.
- The next line is the reference to a function that calculates the molecular weights of the different elements.
- The next line is the reference to a fprintf function that writes the molecular weights of diferent elements in command window.
- The next line is the reference to a function that writes the molecular weights of diferent elements to a file.
OUTPUT-
- The Screenshot of the command window showing molecular weight of elements
- Screenshot of the window where all the folders are saved.
- Plots for O2, N2 and CO2 species
a) Plots of N2
b) Plots of O2
c) Plots of CO2
File generated
Conclusion- Various plots are obtained
Errors- No major errors occured