Project 1 - Parsing NASA thermodynamic data
Project: Parsing NASA Thermodynamic Data in matlab using Thermo.dat file.
Aim:
To write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species, Molecular Weight and Plot nature of the thermodynamic properties of various species
Procedure:
1) Initially open the given thermodynamic data file and read the data in the file and store the data to the variable F.
- First, in the program, the value of the universal gas constant R is given as 8.314 as an input.
- The given data is then formatted as a thermo.dat file.
2) 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. By using fgetl(), strsplit() and str2num() command find out the global low,mid and high temperatures in the second line.
This is thermo.dat file screenshot
Then calculate the 14 coefficients for all the species in the given thermodynamic data file. In data file each species has three lines in which 14 coefficients are there.
3) The file is read line by line by using the ‘fgetl’ command. All the heading and species related information related in code. The first line represents the title if the file so it is ignored.
4) The next line has the global high, global mid and global low temperature values which is read by using ‘fgetl’ command and split into the three values by using the ‘strsplit’ command and converted into numbers. The next three lines are comments in the file so they are ignored by using fgetl() comand
5) Then a ‘for’ loop is created to loop through the 53 species data given in the file and calculate 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. By using the for loop, calculate the 14 coefficients for all the species.
6) By using strfind() command, find out the number of exponential in first line, by which we can find out the number of coefficient in first line. Then the next 3 lines of the file is read which contains the high and low-temperature data of the particular species.
7) The low temperatures values are stored in variables c1, c2, c3 , c4, c5, c6, c7 and the high temperature values are stored invariables c8, c9, c10, c11, c12, c13, c14. By doing the above steps for line two and three, we can calculate the 14 coefficients for all the species.
- Using the ‘mkdir’ command a new folder is created in the current folder ‘C:\Users\Rajat\Documents\MATLAB\matlab\NASA FILE PARSING THERMO\Species\’ with the same name as the species_name
8) Once you find out the coefficients for a species, then calculate the entropy ,enthalpy and specific heat. First 7 coefficients are low, mid temperature coefficients and next 7 are high temperature coef, then calculate cp, H and s.
9) The next line is the reference to specific heat(Cp) which calculates and returns the value of specific heat using the high and low-temperature values as inputs. Assuming universal gas constant R=8.314 J/mol-k and assuming temperature range from 300 to 5000 take 100 values by linspace() command.
Cp=R(a1+a2T+a3T^2+a4T^3+a5T^4)
Then we use the ‘if else’ conditional statement to check if the temp values are less than or equal to the global mid temperature values and select the appropriate equations with high and low-temperature coefficients is used to calculate the specific heat value denoted by 'Cp' and return that value.
10) The next line is the reference to enthalpy (H) .This uses the high-temperature coefficients, low-temperature coefficients, universal gas constant, and global minimum, global mid and global maximum temperature values as the input parameters.
H=RT(a1+a2T/2+a3T^2/3+a4T^3/4+a5T^4/5+a6T)
11) The next line is the reference to a entropy(S) which calculates and returns the value of entropy using the high and low-temperature values as inputs.
S=R(a1logT+a2T+a3T^2/2+a4T^3/3+a5T^4/4+a7)
12) Once you calculate the entropy,enthalpy and specific heat for all the species, plot the graph between: Temperature vs Entropy, Temperature vs Enthalpy and Temperature vs Specific heat. The next line is the reference to plots graphs using the given inputs.
13) Store all the three plots for all the species in the file. The stored file is shown in the above picture
- The first figure(1) represents the plots the temp vs specific heat values.
- The first figure(2) represents the plots the temp vs enthalpy values.
- The first figure(3) represents the plots the temp vs entropy values.
- Using the ‘saveas’ command the three figures are saved in the new folder which is created by the species name.
- Using the ‘cd’ command the folder is changed back to the original folder.
14) Store all the three plots for all the species in the file. The stored file is shown in the above picture. The function runs for 53 times through the ‘for’ loop and it creates separate folders for each species and saves all the plots in the folders.
MAIN PROGRAM
close all
clear all
clc
%Opening and reading the required file
F = fopen('thermo.dat','r')
%Heading
line1 = fgetl(F)
%Global temperature values
line2 = fgetl(F)
%Next three lines are comments
line3 = fgetl(F)
line4 = fgetl(F)
line5 = fgetl(F)
A = strsplit(line2,' '); %' ' = delimeteres
global_low_temp = str2num(A{2})
global_mid_temp = str2num(A{3})
global_high_temp = str2num(A{4})
%species information
for i=1:53
species= fgetl(F);
species_name = species(1:6)
Line_1 = fgetl(F);
L1 = strfind(Line_1,'E');
c1 = str2num(Line_1(1:(L1(1)+3)));
c2 = str2num(Line_1((L1(1)+4):(L1(2)+3)));
c3 = str2num(Line_1((L1(2)+4):(L1(3)+3)));
c4 = str2num(Line_1((L1(3)+4):(L1(4)+3)));
c5 = str2num(Line_1((L1(4)+4):(L1(5)+3)));
Line_2 = fgetl(F);
L2 = strfind(Line_2,'E');
c6 = str2num(Line_2(1:(L2(1)+3)));
c7 = str2num(Line_2((L2(1)+4):(L2(2)+3)));
c8 = str2num(Line_2((L2(2)+4):(L2(3)+3)));
c9 = str2num(Line_2((L2(3)+4):(L2(4)+3)));
c10 = str2num(Line_2((L2(4)+4):(L2(5)+3)));
Line_3 = fgetl(F);
L3 = strfind(Line_3,'E');
c11 = str2num(Line_3(1:(L3(1)+3)));
c12 = str2num(Line_3((L3(1)+4):(L3(2)+3)));
c13 = str2num(Line_3((L3(2)+4):(L3(3)+3)));
c14 = str2num(Line_3((L3(3)+4):(L3(4)+3)));
%creating folder for each species
mkdir(['C:\Users\Rajat\Documents\MATLAB\matlab\NASA FILE PARSING THERMO\Species\',species_name]);
%Temperature ranges assumed for calculation purpose
Temperature = linspace(300,5000,100);
%Universal gas constant
R =8.314;
%calculation of properties based on the lower and upper temperature
for j = 1:length(Temperature)
if Temperature(j)<3000
Cp = (c1+c2.*Temperature+c3.*Temperature.^2+c4.*Temperature.^3+c5.*Temperature.^4).*R;
H = (c1+(c2.*Temperature)./2+(c3.*Temperature.^2)/3+(c4.*Temperature.^3)/4+(c5.*Temperature.^4/5)+c6./Temperature).*Temperature.*R;
S = (c1*log(Temperature)+c2.*Temperature+(c3.*Temperature.^2)/2+(c4.*Temperature.^3)/3+(c5.*Temperature.^4)/4+c7).*R;
else
Cp = (c8+c9.*Temperature+c10.*Temperature.^2+c11.*Temperature.^3+c12.*Temperature.^4).*R;
H = (c8+(c9.*Temperature)/2+(c10.*Temperature.^2)/3+(c11.*Temperature.^3)/4+(c12.*Temperature.^4/5)+c13./Temperature).*Temperature.*R;
S = (c8.*log(Temperature)+c9.*Temperature+(c10.*Temperature.^2)/2+(c11.*Temperature.^3)/3+(c12.*Temperature.^4)/4+c14).*R;
end
end
%plotting
%Temperature vs Entropy
figure(1)
plot(Temperature,S,'linewidth',4,'color','r')
title(species_name)
xlabel('Temperature')
ylabel('Entropy')
%Temperature vs Enthalpy
figure(2)
plot(Temperature,H,'linewidth',4,'color','k')
title(species_name)
xlabel('Temperature')
ylabel('Enthalpy')
%Temperature vs Specific heat
figure(3)
plot(Temperature,Cp,'linewidth',4,'color','y')
title(species_name)
xlabel('Temperature')
ylabel('Specific heat')
directory = (['C:\Users\Rajat\Documents\MATLAB\matlab\NASA FILE PARSING THERMO\Species\',species_name]); %changing the filepath to save the plots
cd(directory)
%saving the plots
saveas(1,'Temp Vs Entropy.jpg');
saveas(2,'Temp Vs Enthalpy.jpg');
saveas(3,'Temp Vs Specific heat.jpg');
cd('C:\Users\Rajat\Documents\MATLAB\matlab\NASA FILE PARSING THERMO\') %switching the directory back to read thermodat file for molecular mass calculation
MM = molecular_mass(species_name)
end
fclose(F)
Function to calculate the molecular weight of all elements: -
This function for molecular weight calculations requires 'species name' as input argument and it is saved in the variable 'MM' and the output argument is molecular mass.
- 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.
- Constituents = ['H', 'C', 'N', 'O', 'Ar'];
- Then used ‘strcmp’ command to compare the name of the element with the string array.
- If there is a repetition of an element, ‘str2num’ is used to find that and (n-1) is multiplied to Atomic_weight of the element to calculate molecular weight.
% Function to calculate the molecular masses of individual species
function MM = molecular_mass(species_name)
species = upper(species_name);
Constituents = ['H', 'C', 'N', 'O', 'Ar'];
Atomic_weight = [1.00794 12.0107 14.0067 15.9994 39.948];
MM = 0;
for i=1:length(species)
for j=1:length(Constituents)
if strcmp(species(i),Constituents(j))
MM = MM + Atomic_weight(j);
u = j; %storing the position of matched element, position= u
end
end
n = str2num(species(i));
if n>1
MM = MM + Atomic_weight(u)*(n-1);
end
end
end
OUTPUT FOR THE PROJECT:
1. Screenshot of the windows where all the folders are saved:-
2. Mollecular masses displayed in window
Plots for O2
Plots for N2
Plots for CO2
Errors faced:
- Error resolved by changing mollecular weigh dirctory to matlab current folder
Referrences:
https://in.mathworks.com/products/matlab.html
https://in.mathworks.com/help/matlab/ref/fscanf.html
https://en.wikipedia.org/wiki/Simulink
https://in.mathworks.com/videos/reading-parameters-from-a-file-in-matlab-97125.html?requestedDomain=
Google drive's links for the file:
https://drive.google.com/drive/folders/1SvRSxhfWw2PVfpYnV6GcesMxqywTIQYi?usp=sharing
https://drive.google.com/drive/folders/1s9GqNdEodMLDksop8MGJE9bTA3mIhx5H?usp=sharing