Project 1 - Parsing NASA thermodynamic data
AIM: To parse a thermodynamic file by NASA.
OBJECTIVES: 1) To calculate the molecular weight of each species and display it in the command window.
2) To plot the cp, enthalpy and entropy for the local temperature range specific for each species.
3) To write a function that extracts the 14 co-efficient and calculates enthalpy, entropy and specific heat for all the species in the data file.
4) To save the plot as images with appropriate names and dump them in separate folders for each species with suitable name.
THEORY:
FILE PARSING: File parsing is all about how information can be read from a particular text file or any file of particular format and also how that information can be utilized in a computer program or any type of problem. Apart from that, we can include data from external databases in our calculations.
SPECIFIC HEAT (cp): The amount of heat required per unit mass to raise the temperature by one degree Celsius is known as specific heat.
ENTROPY(S): Entropy tells about the degree of randomness in the thermodynamic system. Also, it is an extensive property of thermodynamic system.
ENTHALPY(H): It is technically a form of energy and can be defined as heat flow at constant pressure. In thermodynamic system, enthalpy is sum of internal energy as well as product of pressure and volume.
THE FORMULA PROVIDED BELOW THAT IS NEEDED TO BE EXTRACTED:
This formula help us calculate enthalpy,entropy as well as specific heat.
Here the temperature will vary for each species.
To calculate the molecular weight of each species, we make sue of the formula,
Molecular weight=no. of atoms*atomic weight(where we calculate the molecular weight of all species.
WRITING A FUNCTION FOR THE ABOVE FORMULA TO EXTRACT THE 14 COEFFICEIENTS:
% SPECIFIC HEAT, ENTROPY AND ENTHALPY CALCULATIONS
% Defining the function to calculate the 14 coefficients values
function [cp,H,S]=inputs(T,R,Tm1,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14)
for i=1:length(T)
if T(i)>Tm1
cp=(c1+c2.*T+c3.*T.^2+c4.*T.^3+c5.*T.^4).*R; % Defining specific heat value
H=(c1+(c2.*T)./2+(c3.*T.^2)./3+(c4.*T.^3)./4+(c5.*T.^4)./5+c6./T).*T.*R; % Defining Enthalpy
S=(c1.*log(T)+c2.*T+(c3.*T.^2)./2+(c4.*T.^3)./3+(c5.*T.^4)./4+c7).*R; % entropy calculations
else
cp=(c8+c9.*T+c10.*T.^2+c11.*T.^3+c12.*T.^4).*R;
H=(c8+(c9.*T)./2+(c10.*T.^2)./3+(c11.*T.^3)./4+(c12.*T.^4)./5+c13./T).*T*R;
S=(c8.*log(T)+c9.*T+(c10.*T.^2)./2+(c11.*T.^3)./3+(c12.*T.^4)./4+c14).*R;
end
end
end
By using the above functions program, we can calculate specific heat, enthalpy and entropy and here above we are using for loop operations to do the calculations for each species.
Also we are using if else operators, to calculate high temperature for the first 7 elements and and low temperatures for the last 7 elements.
PROGRAM2:-
% MOLECULAR WEIGHT CALCULATIONS
function molecular_weight_calculation=molecular_weight(sp_name)
species=(sp_name);
% ELEMENTS SYMBOLS USED FOR CALCULATIONS
elements=['H','C','O','N','A','S'];
atomic_weight=[1.00794 12.0107 15.999 14.0067 39.948 32.065];
molecular_weight_calculation=0;
for i=1:length(species)
for j=1:length(elements)
if strcmp(species(i),elements(j))
molecular_weight_calculation=molecular_weight_calculation+atomic_weight(j);
a=j;
end
end
n=str2num(species(i));
if n>i,j
molecular_weight_calculation=molecular_weight(sp_name)+atomic_weight(a)*(n-1);
end
end
end
-From the above function program in matlab, we can examine the molecular weight of all the species. Atomic weight as well as atomic names are given as input to the species.
- Here i used for loop to calculate the molecular weight of each species individually. STRCMP command compare the species with the elements and calculate the molecular weight.
PROGRAM 3:-
PARSING NASA THERMODYNAMIC FILE
clear
close all
clc
% EXTRACTING THE DATA FROM THE FILE
f1=fopen('THERMO3.dat','r');
fgetl(f1);
tline=fgetl(f1);
A=strsplit(tline,' ');
% DEFINING THE TEMPERATURES
global_low_temp=str2double(A{2});
global_mid_temp=str2double(A{3});
global_high_temp=str2double(A{4});
%Estimating the lines
for i=3:5
b=fgets(f1);
end
%Getting the temperature and coefficients for each species
for j=1:53
species=fgetl(f1);
t=strfind(species,'.');
%Local temperature for each species
species(52-3:52+3);
Tl=species(52-3:t(1)+3);
Tl1=str2double(Tl);
species(t(1)+6:62+3);
Th=species(t(1)+6:t(2)+3);
Th1=str2double(Th);
species(t(2)+6:72+3);
Tm=species(t(2)+6:t(3)+3);
Tm1=str2double(Tm);
sp_name=species(1:6);
%Coefficients for each species
coeff_l_1=fgetl(f1);% for line 1 = l_1
A11=strfind(coeff_l_1,'E');
coeff_l_1(1:12+3);
a1=coeff_l_1(1:A11(1)+3);
c1=str2double(a1);
a2=coeff_l_1(A11(1)+4:A11(2)+3);
c2=str2double(a2);
a3=coeff_l_1(A11(2)+4:A11(3)+3);
c3=str2double(a3);
a4=coeff_l_1(A11(3)+4:A11(4)+3);
c4=str2double(a4);
a5=coeff_l_1(A11(4)+4:A11(5)+3);
c5=str2double(a5);
coeff_l_2=fgetl(f1); % extracting the line 2= l_2
A12=strfind(coeff_l_2,'E');
coeff_l_2(1:12+3);
a6=coeff_l_2(1:A12(1)+3);
c6=str2double(a6);
a7=coeff_l_2(A12(1)+4:A12(2)+3);
c7=str2double(a7);
a8=coeff_l_2(A12(2)+4:A12(3)+3);
c8=str2double(a8);
a9=coeff_l_2(A12(3)+4:A12(4)+3);
c9=str2double(a9);
a10=coeff_l_2(A12(4)+4:A12(5)+3);
c10=str2double(a10);
%EXTRACTING THE LINE 3= l_3
coeff_l_3=fgetl(f1);
A13=strfind(coeff_l_3,'E');
coeff_l_3(1:12+3);
a11=coeff_l_3(1:A13(1)+3);
c11=str2double(a11);
a12=coeff_l_3(A13(1)+4:A13(2)+3);
c12=str2double(a12);
a13=coeff_l_3(A13(2)+4:A13(3)+3);
c13=str2double(a13);
a14=coeff_l_3(A13(3)+4:A13(4)+3);
c14=str2double(a14);
%MAKING FOLDERS FOR SAVING THE PLOTS
mkdir('F:\matlab\NASA FILE PARSING\Species',sp_name)
% EVALUATING THE ENTROPY, ENTHALPY AS WELL AS THE SPECIFIC HEAT
T=linspace(Tl1,Th1,100);
R=8.314;
[cp,H,S]=inputs(T,R,Tm1,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14);
%PLOTTING
figure(1)
plot(T,cp,'linewidth',2,'color','g')
title(sp_name)
xlabel('temperature')
ylabel('specific heat')
figure(2)
plot(T,S,'linewidth',2,'color','r')
title(sp_name)
xlabel('temperature')
ylabel('entropy')
figure(3)
plot(T,H,'linewidth',2,'color','b')
title(sp_name)
xlabel('temperature')
ylabel('enthalpy')
%CHANGING THE DIRECTORY
program=(['F:\matlab\NASA FILE PARSING\Species\',sp_name]);
cd(program)
%SAVING THE PLOTS
saveas(1,'specific heat.jpg')
saveas(2,'entropy.jpg')
saveas(3,'enthalpy.jpg')
cd('F:\matlab\NASA FILE PARSING')
%Calculating the molecular weight for each species
molecular_weight_calculation=molecular_weight(sp_name)
end
fclose(f1);
EXPLANATION OF THE CODE :-
1) Firstly, file will be opened through fopen command and the information will be extracted from it using fgetl command.
2) Now, global temperatures are definded using strsplit command and fget command used to skip lines.
3) For loop commands are used to get all the temperature and coefficients.
4) Data extracted from the file is in the form of string format and is need to be converted into numbers for which str2double command is used in the program and using this command we will get the all the temperature coefficient values.
5) IF command used to create files for each species.
6)Further, specific heat, enthalpy and entropy values are evaluated and plotted using plot command.
7) Directory is changed using cd command and then plots are saved as images using save command.
8) Finally, we changed the directory again to calcualte molecular weight of each species with the help of function program.
ERRORS FACED:-


Both the above errors are eliminated using str2double command.

For this, i made changes in the mkdir command line to overcome this error.
SCREENSHOT OF WINDOW WHERE ALL FOLDERS ARE SAVED:

PLOTS FOR O2:-



PLOTS FOR N2:-



PLOTS FOR CO2 SPECIES:-



MOLECULAR WEIGHT CALCULATION FOR EACH SPECIES DISPLAYED ON COMMAND WINDOW:-


