Project 1 - Parsing NASA thermodynamic data
AIM: Write a code in MATLAB to parse NASA Thermodynamics data file ,Then calculate molecular weight of each species & plot graph for cp, enthalpy & entropyfor local temperature range.
FILE PARASING
Parasing is a process of analyzing a string of symbols, either in natural language,computer language or data structures. Within computational linguistics the term is used to refer to the formal analysis bya computer of a sentence or other string of words into its constituents, resulting in a parse tree showing a syntactic relation to each other, which may also contain semantic and other information .In generaly it means reading in a data stream of some sort and building an in memory model of semantic content of data. the aims faciltating performing some kind of transformation on the data.
NASA FILE
File contains 53 species and temperature range given and each species contains 14 coefficients and we have to parse 14 coefficients for calculating thermodynamics file.
HERE
R= universal gas constant
T= temperature
cp=Specific heat
S= Entropy
H=ENthalpy
a(i)= coefficients where i= 1 to 14
Specific Heat(cp)
The specific heat is amount of heat per unit mass required to raise the temperature by degreee celcius. its relation is as follows:
Cp= R*(a1 +(a2*t) +(a3(t).^2)+(a4*(t).^3)+(a5*(t).^4))
ENTROPY
It is the measure of a system thermal energy per unit temperature that is unavaliable for doing useful work. As work is obtained form ordered molecular motion, the amount of entropy is also a measure of molecular disorder,or randomness of system.
S=R*(a1*log(t)+a2*t+a3*(t).^2/2+a4*(t).^3/3+a5(t).^4/4+a7)
ENTHALPY
A property of thermodynamic system is equal to the systems internal energy plus the product of its pressure and volume. In a system contained so as to prevent mass transfer for processes at constant pressure, the heat absorbed or released equals the change in enthalpy.
H=R*t(a1+ a2*t/2+ a3*(t).^2/3+ a4*(t).^3/4+ a5(t).^4/5+a6/t)
Main Program
close all
clear all
clc
% opening file for nasa computing data
F1=fopen('THERMO.dat','r');
% to get to next line we use 'fgetl(_)
fgetl(F1);
tline=fgetl(F1);
% to split string so we can use it to assign data
A=strsplit(tline,' ');
% Using str2num coomnd to convert string into number
global_Low_temp= str2num(A{1});
global_Mid_temp= str2num(A{2});
global_HIgh_temp=str2num(A{3});
% values of R gas constant & temperature
R= 8.314;
temp=linspace(global_Low_temp,global_HIgh_temp,1000);
% skipping comments
for i=1:3
gt= fgetl(F1);
end
% to calculate for various values using 'for' loop for 53 items as there
% are 53 components
for i=1:53
gt=fgetl(F1);
A=strsplit(gt,' ');
species= (A{1});
lowtemp=str2num(A{end-3});
midtemp=str2num(A{end-2});
hightemp=str2num(A{end-1});
Line_1= fgetl(F1);
a=strfind(Line_1,'E');
% extracting high coefficients which is from 1 to 7
a1= Line_1(1:a(1)+3);
a1=str2num(a1);
a2= Line_1(a(1)+4:a(2)+3);
a2=str2num(a2);
a3=Line_1(a(2)+4:a(3)+3);
a3= str2num(a3);
a4= Line_1(a(3)+4:a(4)+3);
a4=str2num(a4);
a5= Line_1(a(4)+4:a(5)+3);
a5= str2num(a5);
Line_2=fgetl(F1);
b=strfind(Line_2,'E');
a6=Line_2(1:b(1)+3);
a6=str2num(a6);
a7=Line_2(b(1)+4:b(2)+3);
a7=str2num(a7);
% finding lower coefficients 8 to 14
a8=Line_2(b(2)+4:b(3)+3);
a8=str2num(a8);
a9=Line_2(b(3)+4:b(4)+3);
a9=str2num(a9);
a10=Line_2(b(4)+4:b(5)+3);
a10=str2num(a10);
Line_3=fgetl(F1);
c=strfind(Line_3,'E');
a11=Line_3(1:c(1)+3);
a11=str2num(a11);
a12=Line_3(c(1)+4:c(2)+3);
a12=str2num(a12);
a13=Line_3(c(2)+4:c(3)+3);
a13=str2num(a13);
a14=Line_3(c(3)+4:c(4)+3);
a14=str2num(a14);
% calculating specific heat
cp= specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a12,temp,R,global_Mid_temp);
% calculating entropy
S= entropy_cal(a1,a2,a3,a4,a5,a7,a8,a9,a10,a12,a14,temp,R,global_Mid_temp);
% calculating enthalpy
H= enthalpy_cal(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a12,a13,a14,temp,R,global_Mid_temp);
% calculating atomic Weight
W= Molecular_weight(species);
MW_file= fopen('Molecular_weight.txt','w');
fprintf(MW_file,'Molecular Weight of %s is %d ',species,W);
fclose(MW_file)
cd('C:\Users\rajat\onedrive\desktop\parsing')
mkdir(species);
cd(species);
% plot 1 CP vs t
figure(1)
plot(cp,temp,'linewidth',2,'color','b');
xlabel('Specific heat (KJ)');
ylabel('Temperature');
title('Specific Heat vs Temperature Range',species)
grid on
saveas(figure(1),'specific heat.jpg');
% ploting for S & t
figure(2)
plot(S,temp,'linewidth',2,'color','r');
xlabel('Entropy(KJ/mol-k)');
ylabel('Temperature');
title('Entropy vs Temperature Range', species);
grid on
saveas(figure(2),'entropy.jpg');
% plot for H & t
figure(3)
plot(H,temp,'linewidth',2,'color','g');
xlabel('Enthalpy(KJ/mol)');
ylabel('Temperature');
title('Enthalpy vs Temperature Range', species);
grid on
saveas(figure(3),'enthalpy.jpg')
cd('C:\Users\rajat\onedrive\desktop\parsing')
% molecular weight
W= Molecular_weight(species)
end
STEPS:-
1. Firstly we will input the parameter R and open file thermo.dat using fopen command.
2. Then we read Thermo using fgetl command and then we read temperature file and we will split the temperatures using strsplit command to convert it into cell array and then we will convert this string into number using str2num command to find high,Low& mid global temperatures.
3. We assign temperature range using linspace command and skipp line using fgetl command in loop
4. As our data set is for 53 elements we use for loop for 53 in data file by assigning fgetl command and then splitting it into cell array to find species line and then species name and then number of temperature in species line.
5. Then we read new line in order to read coefficient in data file we first find the 'E' string,Using strfind command to before and after numbers of the coefficients then we will find remaining 14 coefficients and convert them to strings.
6. Out of which we will find 7 coefficients which are for higher temperature and then for second 7 for lower temperature.
7.Now we specify specific heat, enthalpy, entropy functions to find these parameters using 14 coefficients and we specify molecular weight Wto find weight of different elements.
8. Now we write molecular weight.txtfile and fprintf all the molecular weights of species and then close the txt file using fclose command
9. We change directory using cd command to new folder parsing and save file using mkdir command.
10. We plot temperature related to all other parameters and provide data related details using title xlabel or ylabel command.
11. we use saveas commnd to save data needed.
Functions:-
Specific heat
we create function name cp and create condition using if else command to determine data for high and low temperature range coefficients .
% calculating specific heat
function cp= specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a12,temp,R,global_Mid_temp)
% if less thn global mid temperature minimum coefficients are taken
if temp <= global_Mid_temp
cp= R.*( a8 + (a9.*temp) + (a10.*(temp).^2) + (a11.*(temp).^3) + (a12.*(temp).^4));
else
% else maximum coefficients are taken
cp= R*( a1 + (a2.*temp) + (a3.*(temp).^2) + (a4.*(temp).^3) + (a5.*(temp).^4));
end
end
Entropy
we create function name S and create condition using if else command to determine data for high and low temperature range coefficients .
function S= entropy_cal(a1,a2,a3,a4,a5,a7,a8,a9,a10,a12,a14,temp,R,global_Mid_temp)
if temp <= global_Mid_temp
S= R.*( a8.*log(temp) + a9*temp + (a10*(temp).^2/2) + (a11*(temp).^3/3) + (a12*(temp).^4/4) +a14);
else
S= R.*( a1.*log(temp) + a2*temp + (a3*(temp).^2/2) + (a4*(temp).^3/3) + (a5*(temp).^4/4) +a7);
end
end
Enthalpy
we create function name H and create condition using if else command to determine data for high and low temperature range coefficients .
function H= enthalpy_cal(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a12,a13,a14,temp,R,global_Mid_temp)
if temp <= global_Mid_temp
H= R*temp.*(a8+ (a9*temp/2 + a10*(temp.^2)/3 + a11*(temp).^3/4 + a12*(temp).^4/5 + a13*(temp).^5/6) +a14);
else
H= R*temp.*(a1+ (a2*temp/2 + a3*(temp.^2)/3 + a4*(temp).^3/4 + a5*(temp).^4/5 + a6*(temp).^5/6) + a7 );
end
end
Molecular weight
Here we create data set for all main elements base eg:- 'H','C','O','N','AR'.
Then we assign atomic weight to array source in same order. WE will assign molecule staerting range W to zero and create loop condition which runs to length of the species and an another nested for loop is created which runs to length of string atomic_name.We use if else condition to compare species and atomic anme strings to calculate mass by comparing both string found same then we assign weight. We use another str2num to assign to n for complex elemnts weights (CH2NH3 eg) we run in loop in such a way it increment the atomic weight by multiplication of position and (n-1) to the atomic_weight array.
function W = Molecular_weight(species)
% defining atomic names & mass
atomic_names=['H','C','O','N','Ar'];
atomic_weights= [1.008 12.01 15.99 14.00 39.95];
W = 0;
for i=1:length(species)
for j=1:length(atomic_names)
if species(i)==atomic_names(j)
W=W+atomic_weights(j);
position=j;
end
end
n=str2num(species(i));
if n>1
W=W+atomic_weights(position)*(n-1);
end
end
fprintf('Molecular weight of %s - %f',species,W)
disp(' ')
end
Results









