Project 1 - Parsing NASA thermodynamic data
FILE PARSING:
File parsing is the process of extracting the required information from a given file and implementing it in a program.
1) NASA file parsing main program, to get extract 14 coefficients and their respective plots
%opening and reading the NASA thermal data file
file=fopen('THERMO.dat','r');
%Read heading
line1=fgetl(file);
%read global temprature values
line2=fgetl(file);
%splitting the line to get global temp values
A = strsplit(line2,' ');
%reading the global temp values
Global_low_temprature = str2num(A{2});
Global_mid_temprature = str2num(A{3});
Global_high_temprature = str2num(A{4});
%Next three lines are comments from program point of view
comment_1 = fgetl(file);
comment_2 = fgetl(file);
comment_3 = fgetl(file);
%extracing the species information
for i = 1:53
species=fgetl(file);
%reading the species namce as it is present in first six spaces
species_name = species(1:6);
%reading the first coefficents line
Line_1 = fgetl(file);
%identifying the E character so as to differentiate the coefficients
L1 = strfind(Line_1,'E');
%extracting the coefficients based on postiion of E
c1 = str2num(Line_1((L1(1)+4):(L1(2)+3)));
c2 = str2num(Line_1((L1(1)+4):(L1(2)+3)));
c3 = str2num(Line_1((L1(2)+4):(L1(3)+3)));
c4 = str2num(Line_1((L1(3)+4):(L1(4)+3)));
c5 = str2num(Line_1((L1(4)+4):(L1(5)+3)));
%reading the second coefficients line
Line_2 = fgetl(file);
L2 = strfind(Line_2,'E');
c6 = str2num(Line_2(1:(L2(1)+3)));
c7 = str2num(Line_2((L2(1)+4):(L2(2)+3)));
c8 = str2num(Line_2((L2(2)+4):(L2(3)+3)));
c9 = str2num(Line_2((L2(3)+4):(L2(4)+3)));
c10 = str2num(Line_2((L2(4)+4):(L2(5)+3)));
%Reading the first coefficients line
Line_3 = fgetl(file);
L3 = strfind(Line_3,'E');
c11 = str2num(Line_3(1:(L3(1)+3)));
c12 = str2num(Line_3((L3(1)+4):(L3(2)+3)));
c13 = str2num(Line_3((L3(2)+4):(L3(3)+3)));
c14 = str2num(Line_3((L3(3)+4):(L3(4)+3)));
%creating folder for each species
mkdir(['E:\SKILL LYNC\NASA file parsing\species\',species_name]);
%Temprature ranges assumed for calculation purpose
T =linspace(300,5000,100);
%Universal gas constant
R = 8.314;
%calculation of properties based on the lower and upper temprature
for j =length(T)
%first seven coefficients are used if Temp is greater than 3000
if T(j)>3000
Cp =(c1+c2.*T+c3.*T.^2+c4.*T.^3+c5.*T.^4).*R;
H = (c1+(c2.*T)./2+(c3.*T.^2)/3+(c4.*T.^3)/4+(c5.*T.^4/5)+c6./T).*T.*R;
S = (c1*log(T)+c2.*T+(c3.*T.^2)/2+(c4.*T.^3)/3+(c5.*T.^4)/4+c7).*R;
%otherwise the next seven coefficients are used
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
%plotting the properties with respect to local temp values
%teamprature vs Entropy
figure(1)
plot(T,S,'linewidth',4,'color','red')
title(species_name)
xlabel('Temprature')
ylabel('Entropy')
%teamprature vs Enthalpy
figure(2)
plot(T,H,'linewidth',4,'color','blue')
title(species_name)
xlabel('Temprature')
ylabel('Enthalpy')
%teamprature vs specific Heat
figure(3)
plot(T,Cp,'linewidth',4,'color','green')
title(species_name)
xlabel('Temprature')
ylabel('Specific Heat')
%CHANGING THE FILEPATH TO SAVE THE PLOTS CD
saving = (['E:\SKILL LYNC\NASA file parsing\species\',species_name]);
cd(saving)
%saving the plots
saveas(1,'Entropy.jpg');
saveas(2,'Enthalpy.jpg');
saveas(3,'Specific Heat.jpg');
%Calculating the molecular mass of each species
M_M_ = Molecular_mass(species_name);
Species = species_name;
end
fclose(file);
PROCESS EXPLAINED
Starting with clear all, close all and clc
In the next step, NASA thermodat file is opened using the command fopen. the letter r after the file indicates read mode.
Using the fgetl command, we extract each line of the file into the command window. The first line being the header info followed by global temperature values.
The global temperature values obtained.
In order to convert them into numbers, str2num command is used.
From the 6th line onwards, we have the 53 species name and their 14 coefficients. Using the for loop, the name of the species and its coefficients are extracted.
The first fgetl command in the loop is for the species name.
The next three fget is for the the coefficient values.
The strfind command helps us to locate the letter E(exponential)
The mkdir command creates a folder for saving the plots
A range of temperature is assumed for calculating enthalpy,entropy and specific heat values.
In the next step, the for loop determines these parameters for each temperature values.
With the help of plot command, the calculated parameters are plotted against the corresponding value of temperature.
The file directory is then changed to the one that was created in the 9th step to save these plots.
Saveas command helps us in saving these plots The directory is then switched back for molecular mass calculation.
Next line is a function call to calculate the molecular mass.
Program for Molecular mass calculation:
%defining the function
function MM = Molecular_mass(species_name)
%talking input as the specied name
species = upper(species_name);
%Defining the elements of species
Constituents = ['H', 'C', 'N','O','A'];
%Defining the atomic wt of elements
Atomic_weight = [1 12 14 16 40];
MM = 0;
%calculating the moelcular mass based on elements in species
for i = 1:length(species)
for j = 1:length(Constituents)
%comparing the species and elements
if strcmp(species(i),Constituents(j))
MM = MM + Atomic_weight(j);
u = j;
end
end
n = str2double(species(i));
if n>1
MM = MM + Atomic_weight(u)*(n-1);
end
end
end
FILES GENERATED:
1) O2 plots:
ENTHALPY
ENTROPY
SPECIFIC HEAT
2)CO2 PLOTS
ENTHALPY
ENTROPY
SPECIFIC HEAT
3)N2 PLOTS
ENTHALPY
ENTROPY
SPECIFIC HEAT
ERROR OCCURED:
Solution for the above problems:
1) changing the file location and then using mkdir command
2) use of paranthesis
3) cd command used while saving