Project 1 - Parsing NASA thermodynamic data
Aim: -
To write a code in MATLAB to parse the NASA thermodynamic data file and then calculate specific heat,enthaply , entropy, Molecular Weight and Plot nature of the thermodynamic properties of various species w.r.t temperature.
Objectives: -
- To write a function that extracts the 14 co-efficient for a given species from the given thermo.dat file
- To extract the 7 low temperature and 7 high temperature coeffiecients and then using certain formulas to calculate the thermodynamic properties.
- To plot the Cp, Enthalpy, and Entropy of an input species.
- To write a function that calculates the molecular weight of each species.
Procedure:-
- First, in the program, the value of the universal gas constant R is given as 8.314 as an input.
- The ‘fopen’ command was to open the file ('Thermo.DAT) and read mode was specified by 'r' keyword in the ‘fopen’ command.
- The file is read line by line by using the ‘fgetl’ command. The first line represents the HEADER .
- The next line has the global maximum, global mid and global minimum values of temperatures which is read by using ‘fgetl’ command and was split into three values by using the ‘strsplit’ command and converted into numbers using str2num command and were stored in different variables.
- The next three lines are comments in the file so they are ignored by using a ‘for’ loop to iterate for 3 values.
- 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.
- The name of the species is stored in a variable using ‘strtok’ command.
- Then using strfind command '.' is found among the lines and then considering the values right and left of the decimal point the required LOCAL temperature values were extracted.
- Then using findstr command the place of E was extracted from lines and saved into the variable e then interating to the left and right sides of E coefficients were extracted and saved into the variables.a1,a2,a3,a4,a5,a6 and a7 are the high coefficients and a8,a9,a10,a11,a12,a13 and a14 were the low temperature coefficients.
- The read lines are in the form of a string so they are converted into numbers using the ‘str2num’ command and stored in specific variables so they can be used for further calculations.
- Using linspace command the low temperature and high temperature array is saved using local temperatures.
- The specific heat ,enthalpy and entropy was calculate using formula:
- The next line is to create a directory and save it in the names of species cd is used to go to the directory
2. The next lines are to plot the values and save these figures in respective species directory
3.The next line is the reference to a function that calculates the molecular weights of the different elements
- This function takes the species_name as input , The species and the atomic weight were taken as input then looping through the length of input attribute (species_name) and species the strings are compared using strcmp command and if matched the molecular weigt is calculated using the atomic weight values and position is saved in a variable.
- If the species_name contains more than one species the atomic weight is multiplied with (n-1) value and the molecular weight is incremented.
6. The molecular weights are calculated and displayed in command window for all the 53 species
7.The NASA thermodynamic data file is closed using the ‘fclose’ command.
MAIN CODE
clear all
close all
clc
f1=fopen('THERMO.dat','r');
header=fgetl(f1);
R=8.314; %J/KmolK
% Global temperature
tline1=fgetl(f1);
A=strsplit(tline1,' ');
global_lowtemp=str2num(A{2});
global_midtemp=str2num(A{3});
global_hightemp=str2num(A{4});
% Skipping the lines
for i=1:3
fgetl(f1);
end
for i=1:53
%LOCAL TEMPERATURES
tline2=fgetl(f1);
species_name=strtok(tline2);
a=strfind(tline2,'.');
lt_low=str2num(tline2(a(1)-3:a(1)+3));
lt_high=str2num(tline2(a(2)-4:a(2)+3));
lt_mid=str2num(tline2(a(3)-4:a(3)+3));
% there are 53 species
% finding out the coefficients from 1st line
tline3=fgetl(f1);
e=findstr(tline3,'E');
a1=str2num(tline3(1:e(1)+3));
a2=str2num(tline3(e(1)+4:e(2)+3));
a3=str2num(tline3(e(2)+4:e(3)+3));
a4=str2num(tline3(e(3)+4:e(4)+3));
a5=str2num(tline3(e(4)+4:e(5)+3));
% finding out the coefficients in 2nd line
tline4=fgetl(f1);
e=findstr(tline4,'E');
a6=str2num(tline4(1:e(1)+3));
a7=str2num(tline4(e(1)+4:e(2)+3));
a8=str2num(tline4(e(2)+4:e(3)+3));
a9=str2num(tline4(e(3)+4:e(4)+3));
a10=str2num(tline4(e(4)+4:e(5)+3));
% finding out the coefficients in 3rd line
tline5=fgetl(f1);
e=findstr(tline5,'E');
a11=str2num(tline5(1:e(1)+3));
a12=str2num(tline5(e(1)+4:e(2)+3));
a13=str2num(tline5(e(2)+4:e(3)+3));
a14=str2num(tline5(e(3)+4:e(4)+3));
low_temp=linspace(lt_low,lt_mid,100);
high_temp=linspace(lt_mid,lt_high,100);
% Cp value calculation
Cp_High = (a1 + a2*high_temp + a3*high_temp.^2 + a4*high_temp.^3 + a5*high_temp.^4 )*R;
Cp_Low = (a8 + a9*low_temp + a10*low_temp.^2 + a11*low_temp.^3 + a12*low_temp.^4 )*R;
%Enthalphy of the given data
H_High = (a1 + a2*(high_temp/2) + a3*((high_temp.^2)/3) + a4*((high_temp.^3)/4) + a5*((high_temp.^4)/5)+(a6./high_temp))*R.*high_temp;
H_Low = (a8 + a9*(low_temp/2) + a10*((low_temp.^2)/3) + a11*((low_temp.^3)/4) + a12*((low_temp.^4)/5)+(a13./low_temp))*R.*low_temp;
%Entrophy Calculation for the given data
S_High = (a1.*log(high_temp) + a2.*high_temp + (a3.*(high_temp.^2))/2 + (a4.*(high_temp.^3))/3 + (a5.*(high_temp.^4))/4 + a7)*R;
S_Low = (a8.*log(low_temp) + a9.*low_temp + (a10.*(low_temp.^2))/2 + (a11.*(low_temp.^3))/3 + (a12.*(low_temp.^4))/4 + a14)*R;
mkdir(['F:',species_name])
cd(['F:',species_name])
figure(1)
plot(low_temp,Cp_Low,'linewidth',3)
hold on
plot(high_temp,Cp_High,'linewidth',3)
xlabel('Temperature')
ylabel('Specific Heat')
legend('low_temp','high_temp')
hold off
saveas(gcf,'t_vs_Cp.png')
figure(2)
plot(low_temp,H_Low,'linewidth',3)
hold on
plot(high_temp,H_High,'linewidth',3)
xlabel('Temperature')
ylabel('Enthalpy')
legend('low_temp','high_temp')
hold off
saveas(gcf,'t_vs_H.png')
figure(3)
plot(low_temp,S_Low,'linewidth',3)
hold on
plot(high_temp,S_High,'linewidth',3)
xlabel('Temperature')
ylabel('Entropy')
legend('low_temp','high_temp')
hold off
saveas(gcf,'t_vs_S.png')
cd ..
Molecular_Weight(i) = molecular_weight(species_name) ;
strcat( 'The molecular weight of ',' ',species_name, ' ','is' ,num2str(Molecular_Weight(i)) )
end
fclose(f1)- DIRECTORY CONTAINING ALL SPECIES FILES
- OUTPUT FOR O2:
Similarly all the plots are saved onto their respective directories.