Parsing NASA thermodynamic data
AIM: To file parsing of nasa thermodynamic file and to find the molecular weight of O2,N2,CO2.
OBJECTIVES:
1. Write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data 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 (low temperature : high temperature) specific for each species.
4. Save the plots as images with appropriate names and dump them in separate folders for each species with suitable name.
THEORY:
FILE PARSING
Parsing in computer languages refers to syntactic analysis of the input code into its components parts in order to facilitate the writing of compilers and interpreters. It can be used to describe split or separation.Parsing a file means reading in a data stream of some sort and building an in memory model of the semantic content of that data. This aims at facilitating perfoming some kind of transformation on the data.
SPECIFIC HEAT
The specific heat capacity of a substance is the heat capacity of a sample of the substance divided by the mass of the sample. Informally, it is the amount of energy that must be added, in the form of heat, to one unit of mass of the substance in order to cause an increase of one unit in its temperature.
The specific heat of a substance, especially a gas, may be significantly higher when it is allowed to expand as it is heated (specific heat at constant pressure) than when is heated in a closed vessel that prevents expansion (specific heat at constant volume). These two values are usually denoted by and
, respectively; their quotient
is the heat capacity ratio.
For the noble gases, from helium to xenon, these computed values are
| Gas | He | Ne | Ar | Kr | Xe |
|---|---|---|---|---|---|
| 4.00 | 20.17 | 39.95 | 83.80 | 131.29 | |
| 3118 | 618.3 | 312.2 | 148.8 | 94.99 | |
| 5197 | 1031 | 520.3 | 248.0 | 158.3 |
ENTHALPY:
Enthalpy, a property of a thermodynamic system, is equal to the system\'s internal energy plus the product of its pressure and volume. In a system enclosed so as to prevent mass transfer, for processes at constant pressure, the heat absorbed or released equals the change in enthalpy.
The enthalpy of a thermodynamic system is defined as
- H = U + pV
where
- H = enthalpy
- U = internal energy of the system
- p = pressure,
- V = volume of the system
ENTROPY:
Entropy is an extensive property of a thermodynamic system. It is closely related to the number Ω of microscopic configurations that are consistent with the macroscopic quantities that characterize the system.
MOLECULAR WEIGHT:
The molecular mass is the mass of a given molecule: it is measured in daltons. Different molecules of the same compound may have different molecular masses because they contain different isotopes of an element.
Molecular masses are calculated from the atomic masses of each nuclide present in the molecule, while molar masses are calculated from the standard atomic weights of each element. The standard atomic weight takes into account the isotopic distribution of the element in a given sample (usually assumed to be \"normal\"). For example,water has a molar mass of 18.0153(3) g/mol, but individual water molecules have molecular masses which range between 18.010 564 6863(15) Da (1H
216O) and 22.027 7364(9) Da (2H
218O).
FORMULA FOR EQUATING THE FUNCTION:
The formulae are shown below,
- H = enthalpy
- R = universal gas constant(8.314 J/(mol-k))
- S = entropy
- CP = specific heat
- T = temperature
where (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14) are the coefficients of each species.
Different functions is been created and all will be calling in the main program
- specific heat
- enthalpy
- entropy
- molecular weight
Syntax used in the program
1. f1=fopen(\'THERMO.dat\',\'w\');
To open the data that are given in the challenges and \'w\' is used to write in the file.
f1=fopen(\'THERMO.dat\',\'r\');
To open the data that are given in the challenges and \'r\' is used to read in the file.
2.\'THERMO.dat\'
It is used to check whether the program name is initiate as the same name of the file.
3. fget(f1)
first_line=fgetl(f1);
This is used to show the first line of data in the command window if you again use fget(f1) and it show next line and it goes on.
Some matlab commands which is used in this program
- strsplit: split the character vertor that contains comma-seperated values.
- str2double: It converts the string into double precision values.
- strfind: This command is used to find out any letter or point or anything in between the lines.
- mkdir: This is basically used to create the folder through which the file is going to be saved.
- cd: Basically cd is used to display the current folder.
- fscanf: it is used normally when the file is going to read so at that time it is been used to scan the particular thing but you cannot change it because your file is open in the read mode.
- fprintf: it is used normally when the file is going to write so at that time it is been used to basically.
FUNCTIONS USED IN THAT PROGRAM
1. For Specific heat
[C_P]=specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,low_temp,mid_temp,high_temp,T);
2. For Enthalpy
[H]=enthalpy_func(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,low_temp,mid_temp,high_temp,T);
3. For Entropy
[S]=entropy_func(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,low_temp,mid_temp,high_temp,T);
4. For Molecular weight
[Mol_wt]=molecular_weight(species_name);
NOTE:
- Now plots are plotted and labelled as the specific heat,enthalpy,entropy and temperature.they presented by figure(1), figure(2), figure(3).
- image=getframe(gcf) this command is used to get the frame of the image that obtain by in the plot.
- This command is used to save the image in the folder
imwrite(image.cdata,\'specific_heat vs temperature.png)
imwrite(image.cdata,\'enthalpy vs temperature.png)
imwrite(image.cdata,\'entropy vs temperature.png)
- Molecular weight are calculated by using the atomic mass and molecular weight of each species.
- In next step calling the molecular function program and to calculate the molecular weight.
- By using the species and the elements in the \"for loop\"and \"if\" condition and substitute the atomic values in the molecular weight formula.
- After finding the molecular weight return to the main program and display the molecular weight for all the species.
- Finally run the program to get the specific heat(C_P), enthalpy(H), entropy(S) and the molecular weight(Mol_wt) values for all the species and also plot the (C_P), (H), (S) for temperature.
FUNCTION PROGRAMS
1.Specific heat[C_P]
function [C_P]=specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,low_temp,mid_temp,high_temp,T);
if (T>mid_temp)
C_P=(a1+(a2*T)+(a3*(T.^2))+(a4*(T.^3))+(a5*(T.^4)));
else
C_P=(a8+(a9*T)+(a10*(T.^2))+(a11*(T.^3))+(a12*(T.^4)));
end
2.Enthalpy[H]
function [H]=enthalpy_func(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,low_temp,mid_temp,high_temp,T);
if (T>mid_temp)
H=(a1*T+(a2*(T.^2)/2)+(a3*(T.^3)/3)+(a4*(T.^4)/4)+(a5*(T.^5)/5)+a6);
else
H=(a8*T+(a9*(T.^2)/2)+(a10*(T.^3)/3)+(a11*(T.^4)/4)+(a12*(T.^5)/5)+a13);
end
3.Entropy[S]
function [S]=entropy_func(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,low_temp,mid_temp,high_temp,T);
if (T>mid_temp)
S=(a1*log(T)+(a2*T)+(a3*(T.^2)/2)+(a4*(T.^3)/3)+(a5*(T.^4)/4)+a7);
else
S=(a8*log(T)+(a9*T)+(a10*(T.^3)/3)+(a12*(T.^4)/4)+a14);
end
4.Molecular weight[Mol_wt]
%molecular mass calculation
function [Mol_wt]=molecular_weight(species_name);
Elements=[\'H\',\'C\',\'O\',\'N\',\'A\'];
Atomic_mass=[1.0079 12.0107 15.0999 14.0067 39.948];
Mol_wt=0;
z=0;
for i=1:length(species_name)
for j=1:length(Elements)
if strcmpi(species_name(i),Elements(j))
Mol_wt=Mol_wt+Atomic_mass(j);
z=j;
end
end
n=str2double(species_name(i));
if (n>1)
Mol_wt=Mol_wt+(Atomic_mass(z)*(n-1));
end
end
M=Mol_wt;
end
EXPLAINATION OF THE MAIN PROGRAM
- First step is to read the file that are given in the challenges THERMO.dat by using the syntax.
f1=fopen(\'THERMO.dat\',\'r\').
- Now file are assigned as input data contained various species with different coefficients.
- fgetl(f1) syntax is used for calling the first line of THERMO.dat so that line are called for loop executed for skip the first three lines.
- We want to split the coefficients by the numerical valueso that string split syntax is used strsplit.
- Global temperature(low,mid,high) are also splitted in that data.
- To seperate each coefficients in the species string find command is used
coefficient=fgetl(f1);
A=strfind(coefficient,\'E\');
- By applying this string finds the coefficient value then the formula are assign for the calculating the next coefficient in the same species.
a(1)=(1:12+3)
- Now a(2) are calculated by this equation here str2double is refer as the str2num that syntax seperates the coefficient as the numerical value. a(2)=str2double(coefficient(a(1)+4:a(2)+3))
- Same process are followed for the 14 coefficients in the species.
- Then open an loop for finding the values of all 53 elements to calculate the molecular weight.
- Functions are assigned in different tabs by using formula were given in the challenge.
- Where R is the universal gas constant (8.314 j/(mol-k)),T is the temperature called in the function.
- In each function (if,else) loop is present to determine the low temperature and the high temperature of variable coefficients mid temperature are initiate in (if,else)loop.
(T>mid_temp)
- Functions are calling as by same name for execute the calculation.
MAIN PROGRAM :
clear all
close all
clc
%input data
%to read the data from the data file
f1=fopen(\'THERMO.dat\',\'r\');
%reading the header information
initial_line=fgetl(f1);
first_line=fgetl(f1);
A=strsplit(first_line,\' \');
low_temp=str2double(A{2});
mid_temp=str2double(A{3});
high_temp=str2double(A{4});
%skipping the next three lines
for i=1:3
data=fgetl(f1);
end
%for calculating and reading all the species
%using the for loop
for i=1:53
second_line=fgetl(f1);
S=strsplit(second_line,\' \');
species_name=S{1};
%split the local temp
T=strfind(second_line,\'G\');
temp_values=second_line(T+4:length(second_line));
split_temp=strsplit(temp_values,\' \');
local_low_temp=str2double(split_temp{1});
local_mid_temp=str2double(split_temp{2});
local_high_temp=str2double(split_temp{3});
coefficient=fgetl(f1);
a=strfind(coefficient,\'E\');
%getting first line coefficients of (a1 to a5)
a1=str2double(coefficient(1:a(1)+3));
a2=str2double(coefficient(a(1)+4:a(2)+3));
a3=str2double(coefficient(a(2)+4:a(3)+3));
a4=str2double(coefficient(a(3)+4:a(4)+3));
a5=str2double(coefficient(a(4)+4:a(5)+3));
%getting second linecoefficients of (a6 to a10)
coefficient_1=fgetl(f1);
a=strfind(coefficient_1,\'E\');
a6=str2double(coefficient_1(1:a(1)+3));
a7=str2double(coefficient_1(a(1)+4:a(2)+3));
a8=str2double(coefficient_1(a(2)+4:a(3)+3));
a9=str2double(coefficient_1(a(3)+4:a(4)+3));
a10=str2double(coefficient_1(a(4)+4:a(5)+3));
%getting third linecoeffecients (a11 to a14)
coefficient_2=fgetl(f1);
a=strfind(coefficient_2,\'E\');
a11=str2double(coefficient_2(1:a(1)+3));
a12=str2double(coefficient_2(a(1)+4:a(2)+3));
a13=str2double(coefficient_2(a(2)+4:a(3)+3));
a14=str2double(coefficient_2(a(3)+4:a(4)+3));
r=8.314;
T=linspace(low_temp,high_temp,200);
C_P=r*specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,low_temp,mid_temp,high_temp,T);
H=r*enthalpy_func(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,low_temp,mid_temp,high_temp,T);
S=r*entropy_func(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,low_temp,mid_temp,high_temp,T);
%creating folder
mkdir([\'C:\\Users\\PRAMANSHU\\Documents\\MATLAB\\species_name\\\',species_name]);
cd([\'C:\\Users\\PRAMANSHU\\Documents\\MATLAB\\species_name\\\',species_name]);
%plotting the graph
%to plot the specific heat vs temperature
figure(1);
plot(T,C_P);
grid on
title(\'specific heat vs temperature\')
xlabel(\'temperature\');
ylabel(\'specefic heat\');
image=getframe(gcf);
imwrite(image.cdata,\'specific_heat vs temperature.png\')
%to plot enthalpy vs temperature
figure(2);
plot(T,H);
grid on
title(\'enthalpy vs temperature\')
xlabel(\'temperature\');
ylabel(\'enthalpy\');
image=getframe(gcf);
imwrite(image.cdata,\'enthalpy vs temperature.png\')
%to plot entropy vs temperature
figure(3);
plot(T,S);
grid on
title(\'entropy vs temperature\')
xlabel(\'temperature\');
ylabel(\'entropy\');
image=getframe(gcf);
imwrite(image.cdata,\'entropy vs temperature.png\')
cd([\'C:\\Users\\PRAMANSHU\\Documents\\MATLAB\\species_name\\\',species_name]) ...
%molecular mass calculation
Mol_wt=molecular_weight(species_name);
print=sprintf(\'%s Mol_wt=%f\',species_name,Mol_wt);
disp(print)
end
ERRORS AND RECTIFICATION:
ERROR
Wrong path is given so due to this the file is not saved in the correct path
cd is used to display the folder and also return back to the same folder to open the different files according to the program.
SCREENSHOT OF THE COMMAND WINDOW
According to the program if path is not given correctly so it shows an error because the file is not been saved.
RECTIFICATION
After given the right path then the file is saved and it shows in the folder also.
Now the file will display because the error is been rectified.
OUTPUTS:
SCREENSHOT OF THE FOLDER WHERE THE FILE IS SAVED
Folder Name - species_name
ALL SPECIES MOLECULAR WEIGHT
WORKSPACE OF THAT PROGRAM
GRAPHS OF THE FEW SPECIES
1.O2
For specific heat and temperature
For enthalpy and temperature
For entropy and temperature
2.N2
For specific heat and temperature
For enthalpy and temperature
For entropy and temperature
3.CO2
For specific heat and temperature
For enthalpy and temperature
For entropy and temperature
CONCLUSION
The results were obtained by running the program.File parsing is successfully done and the plots of all the 53 different species were plotted and the respective molecular weigts are calculated.
REFERENCES:
1.https://drive.google.com/file/d/1AikCHDlz9s_fu8whE81qee_GD4AyYpCP/view?usp=sharing
2.Wikipedia.
3.Contact to the instructor for different queries related to that program.
4.Taking help through watching youtube videos.