Project 1 - Parsing NASA's thermodynamic data using MATLAB
Aim:
To write the program for parsing NASA's thermodynamic data to evaluate thermodynamic properties such as Cp, H and S.
Objective:
- To extract the all the 14 coefficients of elements/compound from the data and calculate entropy, enthalpy and specific heat.
- To plot the variation of these parameters with respect to specified local temperature range.
- To calculate the molecular masses of each element/compound .
Formulas:
where,
a1, a2, a3, a4, a5, a6, and a7 are the numerical coefficients supplied in NASA thermodynamic files. The first 7 numbers starting on the second line of each species entry (five of the second line and the first two of the third line) are the seven coefficients (a1 through a7, respectively) for the high-temperature range (above 1000 K, the upper boundary is specified on the first line of the species entry). The following seven numbers are the coefficients (a1 through a7, respectively) for the low-temperature range (below 1000 K, the lower boundary is specified on the first line of the species entry).
The first 5 lines of the file gives information about global Temperature Range.From the 6th line onwards till “END”, the species information are provided.Each species data are provided in 4 lines in a specific format.
click on link below to get the NASA thermodynamic data:
https://drive.google.com/file/d/1AikCHDlz9s_fu8whE81qee_GD4AyYpCP/view
Step wise Explanation of code:
- First open the given thermodynamic data and read the data in a file and store the data in the variable file.
- Then read the header file of the thermodynamic data and calculate the global low, mid and high temperature.
- Certain header lines were skipped which were of no use.
- Using fgetl(), strsplit(), strfind and str2double() the value of local low, mid and high temperature were stored in the variable.
- Then the 14 coefficient of each element were stored in their respective variable by using for loop, fgetl(), strsplit(), strfind functions.
- First the for loop is executed for calculating 14 coefficients of each element.
- Fgetl() function was used to read coefficients in 1st line, then strfind() was used to find the exponent ‘E’ by using this we can store the coefficient in the respective variable.
- Once the coefficient was found out it was converted into decimal number using str2double() function.
- By doing the above steps we can store the coefficients into respective variables.
- Once the coefficients were found out, then we can calculate the specific heat, entropy and enthalpy by taking gas constant R=8.314 J/mol-k and temperature a linspace() function of lower temp. to high temp.
- Once the value of specific heat, entropy and enthalpy is found out now we can plot graphs between specific heat vs temperature, enthalpy vs temperature and entropy vs temperature for each element.
- The three plots were stored in a folder having the name of the element.
- Finally molecular mass of each element was calculated and displayed in command window.
Program:
1. Main program for prasing data:
%Parsing NASA's thermodynamic data
clear all
close all
clc
%finding and opening the file_in_loadpath
file=fopen('THERMO.dat','r');
%header
line1=fgetl(file);
%global temperature values
line2=fgetl(file);
A=strsplit(line2,' ');
global_min_temp=str2num(A{2});
global_mid_temp=str2num(A{3});
global_max_temp=str2num(A{4});
%skipping comments
fgetl(file);
fgetl(file);
fgetl(file);
%using the for loop to consier 53 elements
for i=1:53
line3=fgetl(file); %reading the file
element=line3(1:6); %naming first variable as element
P=strfind(line3,'G');
local_low_temp=line3(P+3:55);
local_high_temp=line3(P+13:65);
local_mid_temp=line3(P+23:75);
lt=str2double(local_low_temp); %creating low temp. variable
ht=str2double(local_high_temp); %creating high temp. variable
mt=str2double(local_mid_temp); %creating mid temp. variable
%line 1 reading coefficients
cfline1=fgetl(file);
X=strfind(cfline1,'E');
a1=cfline1(1:X(1)+3);
a1=str2double(a1); %storing 1st coefficients
a2=cfline1(X(1)+4:X(2)+3);
a2=str2double(a2); %storing 2nd coefficients
a3=cfline1(X(2)+4:X(3)+3);
a3=str2double(a3); %storing 3rd coefficients
a4=cfline1(X(3)+4:X(4)+3);
a4=str2double(a4); %storing 4th coefficients
a5=cfline1(X(4)+4:X(5)+3);
a5=str2double(a5); %storing 5th coefficients
%line 2 reading coefficients
cfline2=fgetl(file);
Y=strfind(cfline2,'E');
a6=cfline2(1:Y(1)+3);
a6=str2double(a6); %storing 6th coefficients
a7=cfline2(Y(1)+4:Y(2)+3);
a7=str2double(a7); %storing 7th coefficients
a8=cfline2(Y(2)+4:Y(3)+3);
a8=str2double(a8); %storing 8th coefficients
a9=cfline2(Y(3)+4:Y(4)+3);
a9=str2double(a9); %storing 9th coefficients
a10=cfline2(Y(4)+4:Y(5)+3);
a10=str2double(a10); %storing 10th coefficients
%line 3 reading coefficients
cfline3=fgetl(file);
Z=strfind(cfline3,'E');
a11=cfline3(1:Z(1)+3);
a11=str2double(a11); %storing 11th coefficients
a12=cfline3(Z(1)+4:Z(2)+3);
a12=str2double(a12); %storing 12th coefficients
a13=cfline3(Z(2)+4:Z(3)+3);
a13=str2double(a13); %storing 13th coefficients
a14=cfline3(Z(3)+4:Z(4)+3);
a14=str2double(a14); %storing 14th coefficients
%Creating the folder for each element
mkdir('c:/users/HP/desktop/matlab course/NASA thermodynamic data',element);
%Universal gas constant
R=8.314;
%Ranging the temperature from low temperature to high temperature
T=linspace(lt,ht,100);
%Specific heat
for j=1:length(T)
if T(j)
2. Function to calculate the molecular mass of element:
%function for calculating molecular mass
function MM=molecular_mass(element)
element_name=upper(element)
atoms={'H','C','N','O','AR'};
atomic_weight=[1 12 14 16 40];
MM=0;
for i=1:length(element)
for j=1:length(atomic_weight)
if strcmp(element_name(i),atoms(j))==1
MM=MM+atomic_weight(j);
u=j;
end
end
%adding the weight of repeatating atom
n=str2double(element_name(i));
if n>1
MM=MM+atomic_weight(u)*(n-1);
end
end
end
Output:
- Location of element folders:
2. Molecular masses displayed in command window:
3. Graphical plot of O2:
4. Graphical plot of N2:
5. Graphical plot of CO2:
Error faced while Programing:
1. At least one 'End' is missing.
>> Solved by placing End command where it was missing by reviewing program.
2.Error in ' line1=fget1(file)'
>> solved by correcting 'fget1' to 'fgetl'
3.Error in path directory of folder.
>> solved by using correct path of folder.
Conclusion:
Thus, the parsing of given NASA thermodynamic data was done.
Referances: