Project 1 - Parsing NASA thermodynamic data
Objective: Parsing Nasa thermodynamic data
1. To extract all 14 coefficients from THERMO.dat file and calculate specific heat(Cp),enthalpy(H) and entropy(S) from given formula
Cp/R = a1+ a2 T +a3 T^2 + a4 T^3 + a5 T^4
H/RT = a1 + a2 T/2 + a3 T^2 /3 + a4 T^3 /4 + a5 T^4/5 +a6/T
S/R = a1 lnT +a2 T + a3 T^2/2 + a4 T^3 /3 + a5 T^4 /4 + a7
The first 7 coefficients are High temperature coefficients and the next 7 coefficients are Low temperature coefficients.
We have to use Local Temperature ranges which are unique for calculations not the global temperature ranges.
2.Plot the Cp, H & S for local temperature range (low temperature : High temperature) specific for each species.
3.Save the plots as images with appropriate name and dump them in seperate folders for each species with suitable name.All this should be genertaed automatically.
4. Calculate the molecular mass of each species and display them in command window
Explaination:
Parsing : Convert a String or Text data into a more usable format.
Here we are parsing a file name "Thermo.dat" using Matlab.
Steps involed while parsing:
1) Firstly we open our Thermo.dat file using 'fopen' command to obtain information of file.
2) We start reading file using 'fgetl' command which read line from file associated with file Identifier.
Header is obtained using fgetl.
we store our next line in 'tline'.And use 'strsplit' with delimiter as "spaces" to get respective global range temperatures.
3) Then we simply read the next 3 lines as they are comments and are to be skipped.
4) Since there are total 53 species we use for loop for main program to get species related information.
5) We store next line in tline_1.Use 'strsplit' to split the string and by specifying delimiter.
we get our species.
6)For next 3 lines of code we extract local range temperatures from the same string 'tline_1'.
7) We store next line in 'tline_2' .
Next line we use 'strfind' to locate string 'E' and by using its position as reference we find coefficients from 3 lines. Then convert those string coefficents to numbers using 'str2double'.
8) We use the given formula to calculate the specific heat(Cp),Enthalpy(H),Entropy(S). using obtained coefficients and range of local temperature.
9) Next we plot those respective obtained properties i.e specific heat, enthalpy & entropy w.r.t temperature.
10) 'cd' command is used to display the current folder.
11) The 'mkdir' command is used to create a folder to save the plots. Next we change our path using 'cd' to loaction where we want save our plot. After saving again we reset our folder to intial apth usign 'cd' so that we can read 'Thermo.dat' file.
12) Two different vectors are formed for "elements (H, O,N etc)" & their atomic weight.
Then we use two for loops one for species and one for elements.'strcmp' is used to compare the two strings i.e species and elements and return an logical output of 1 if they match.
Initial molecular mass is taken as zero so we can store molecular mass for complex species.
Also we have few species which have in which atomic weight has to be multiplied with previous element.
'if' statement is used for the same.
Finally we use 'fclose' to close the file.
Following is the program:
clear all
close all
clc
f1=fopen('THERMO.dat','r') % opens and obtain information about file
file_Id=fgetl(f1); % read a file line by line(getting header)
tline=fgetl(f1);% storing line in variable tline
tline=strsplit(tline,' '); % splitting the strings at specified delimiter
global_low_temp=str2num(tline{2}); % obtaining global temperature
global_mid_temp=str2num(tline{3});
global_high_temp=str2num(tline{4});
for i=1:3
tline=fgetl(f1); % skipping comments
end
for i= 1:53
tline_1=fgetl(f1);
tline_1=strsplit(tline_1,' ');% splitting by specifying space delimiter
species=tline_1{1};% obtaining species
z=length(tline_1);
local_low_temp=str2double(tline_1{z-3}); % converting string to number and storing temperatures
local_high_temp=str2double(tline_1{z-2});
local_med_temp=str2double(tline_1{z-1});
tline_2=fgetl(f1);
B=strfind(tline_2,'E');% finding the position of E in string of line_2
c1=str2double(tline_2(1:B(1)+3)); % obtaining coefficents from tline_2
c2=str2double(tline_2(B(1)+4:B(2)+3));
c3=str2double(tline_2(B(2)+4:B(3)+3));
c4=str2double(tline_2(B(3)+4:B(4)+3));
c5=str2double(tline_2(B(4)+4:B(5)+3));
tline_3=fgetl(f1);
B=strfind(tline_3,'E');
c6=str2double(tline_3(1:B(1)+3)); %obtaining coefficents from tline_3
c7=str2double(tline_3(B(1)+4:B(2)+3));
c8=str2double(tline_3(B(2)+4:B(3)+3));
c9=str2double(tline_3(B(3)+4:B(4)+3));
c10=str2double(tline_3(B(4)+4:B(5)+3));
tline_4=fgetl(f1);
B=strfind(tline_4,'E');
c11=str2double(tline_4(1:B(1)+3)); %obtaining coefficents from tline_4
c12=str2double(tline_4(B(1)+4:B(2)+3));
c13=str2double(tline_4(B(2)+4:B(3)+3));
c14=str2double(tline_4(B(3)+4:B(4)+3));
% Calculation of specific heat,enthalpy,entropy
R=8.314 % Universal gas constant in kj/k-mol k
T=linspace(local_low_temp,local_high_temp,100);% creating a temperature vector of 100 values
T_2=local_med_temp
if T>T_2
C_p=(c1+c2.*T+c3.*(T.^2)+c4.*(T.^3)+c5.*(T.^4))*R; % specific heat formula
H=((c1+c2.*(T/2)+c3.*(T.^2/3)+c4.*(T.^3/4)+c5.*(T.^4/5)+(c6./T)).*T)*R;% entalpy formula
S=(c1.*log(T)+c2.*T+c3.*(T.^2/2)+c4.*(T.^3/3)+c5.*(T.^4/4)+c7)*R;% entropy formula
else
C_p=(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
%plotting properties w.r.t local temperatures
figure(1)
plot(T,C_p,'linewidth',3,'color','r');
xlabel('temperature')
ylabel('specific heat')
title(species)
grid on
figure(2)
plot(T,H,'linewidth',3,'color','b');
xlabel('temperature')
ylabel('entalpy')
title(species)
grid on
figure(3)
plot(T,S,'linewidth',3,'color','g');
xlabel('temperature')
ylabel('entopry');
title(species)
grid on
cd; % it shows current directory
mkdir('F:\plot_properties\',species) % making folder name plot_properties with sub folders as species
%mypath=sprintf('F:/plot_properties/%s',species);
mypath=(['F:\plot_properties\',species]);
cd(mypath) % changing directory to my path
saveas(1,'sp.heat.jpg'); % saving plots in current directory
saveas(2,'enthalpy.jpg');
saveas(3,'entropy.jpg');
cd('F:\skill lync\File parsing') % again setting back the directory to read thermo.dat file
% Molecular mass
elements=[ 'H', 'C','N','O','A']; % listing string of elements in file
Atomic_weight = [1.00794 12.0107 14.0067 15.9994 39.948]; % atomic weight of elements
MM =0; % setting MM=0 for storing complex species molar mass
for i=1:length(species)
for j=1:length(elements)
if strcmp(species(i),elements(j)) % comparing strings of species and elements and getting a logical output
MM = MM + Atomic_weight(j);% molecular mass calculation
position=j;
n = str2num(species(i));
if n>1
MM = MM + Atomic_weight(position)*(n-1);% molecular mass with species having same element more than once
end
end
end
sprintf('Molecular weight %s:%f u',species,MM)% storing value of molecular mass of species
end
end
fclose(f1)
Screenshot of folders:
Plots for O2, N2 & CO2 species:
1) O2
2) N2
3) CO2
Screenshot of Molecular Mass:
Result: The code worked fine. Coefficients are extraxted. Thermodynamic properties and molecular mass has been calculated.
Refrences: Mathwork