Project

NASA FILE PARSING

AIM OF THE PROJECT:

The main objectives of the project are:

1.) To extract 14 coefficients of each species and calculate the entropy, enthalpy and specific heat from the file given.

2.) To show the variation of these parameters against the corresponding temperature.

3.)To calculate the molecular weight of individual species.

FILE PARSING:

File parsing is the process of extracting the required information from a give file and implementing it in a program.

PROGRAM:

1.) Program to extract the 14 coefficients and get the desired plots:

close all
clear all
clc

file = fopen(\'NASA Thermodat\',\'r\')          %Opening and reading the required file

line1 = fgetl(file)                         %Heading
line2 = fgetl(file)                         %Global temperature values

comment_1 = fgetl(file)                     %Next three lines are comments 

comment_2 = fgetl(file)                     %which are redundant 

comment_3 = fgetl(file)                     %from program point of view

A = strsplit(line2,\' \');
Global_low_temperature = str2num(A{2})
Global_mid_temperature = str2num(A{3})
Global_high_temperature = str2num(A{4})

%species information
for i=1:53
    
    species= fgetl(file);
    
    species_name = species(1:6)
    
Line_1 = fgetl(file);
    
L1 = strfind(Line_1,\'E\');

c1 = str2num(Line_1(1:(L1(1)+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)));


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)));


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([\'C:\\Users\\Sagar\\File parsing NASA thermodat\\Species\\\',species_name]);

Temperature = linspace(300,5000,100);  %Temperature ranges assumed for calculation purpose

R =8.314;                              %Universal gas constant

%calculation of properties based on the lower and upper temperature
for j = 1:length(Temperature)
    if Temperature(j)<3000
Cp = (c1+c2.*Temperature+c3.*Temperature.^2+c4.*Temperature.^3+c5.*Temperature.^4).*R;
H = (c1+(c2.*Temperature)./2+(c3.*Temperature.^2)/3+(c4.*Temperature.^3)/4+(c5.*Temperature.^4/5)+c6./Temperature).*Temperature.*R;
S = (c1*log(Temperature)+c2.*Temperature+(c3.*Temperature.^2)/2+(c4.*Temperature.^3)/3+(c5.*Temperature.^4)/4+c7).*R;
    else
Cp = (c8+c9.*Temperature+c10.*Temperature.^2+c11.*Temperature.^3+c12.*Temperature.^4).*R;
H = (c8+(c9.*Temperature)/2+(c10.*Temperature.^2)/3+(c11.*Temperature.^3)/4+(c12.*Temperature.^4/5)+c13./Temperature).*Temperature.*R;
S = (c8.*log(Temperature)+c9.*Temperature+(c10.*Temperature.^2)/2+(c11.*Temperature.^3)/3+(c12.*Temperature.^4)/4+c14).*R;
    end
end

%plotting

%Temperature vs Entropy
figure(1)
plot(Temperature,S,\'linewidth\',4,\'color\',\'b\')
title(species_name)
xlabel(\'Temperature\')
ylabel(\'Entropy\')

%Temperature vs Enthalpy
figure(2)
plot(Temperature,H,\'linewidth\',4,\'color\',\'k\')
title(species_name)
xlabel(\'Temperature\')
ylabel(\'Enthalpy\')

%Temperature vs Specific heat
figure(3)
plot(Temperature,Cp,\'linewidth\',4,\'color\',\'y\')
title(species_name)
xlabel(\'Temperature\')
ylabel(\'Specific heat\')

directory = ([\'C:\\Users\\Sagar\\File parsing NASA thermodat\\Species\\\',species_name]);   %changing the filepath to save the plots
cd(directory)

%saving the plots

saveas(1,\'Entropy.jpg\');
saveas(2,\'Enthalpy.jpg\');
saveas(3,\'Specific heat.jpg\');

cd(\'C:\\Users\\Sagar\\File parsing NASA thermodat\\\')       %switching the directory back to read thermodat file for molecular mass calculation

MM = molecular_mass(species_name)

end

fclose(file)

EXPLAINATION:

1.) The program begins with clear all, close all and clc which clears the workspace, closes all the plots and clears the command window respectively.

2.) In the next step, NASA thermodat file is opened using the command fopen. the letter r after the file indicates read mode.

3.) 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 next three lines are comments which are not required from program point of view.

4.) The global temperature values obtained in the previous step is in the form of strings. In order to convert them into numbers, str2num command is used.

5.) 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.

6.) The first fgetl command in the loop gets us the species name.

7.) The next three fget helps us extract the coefficient values.

8.) The strfind command helps us to locate the letter E(exponential) which is used as reference for gettiing the coefficient values. These values extracted as strings are then converted into numbers using str2num command.

9.) The mkdir command creates a folder for saving the plots that will be generated.

10.) A range of temperature is assumed for calculating enthalpy,entropy and specific heat values.

11.) In the next step, the for loop determines these parameters for each temperature values.

12.) With the help of plot command, the calculated parameters are plotted against the corresponding value of temperature.

13.) The file directory is then changed to the one that was created in the 9th step to save these plots.

14.) Saveas command helps us in saving these plots with the names we want.

15.) The directory is then switched back for molecular mass calculation.

16.) Next line is a function call to calculate the molecular mass.

17.) The file is then closed.

2.) Program to calculate the Molecular mass of each species:

function MM = molecular_mass(species_name)
  
species = upper(species_name); 

Constituents = [\'H\', \'C\', \'N\', \'O\', \'A\'];

Atomic_weight = [1.00794 12.0107 14.0067 15.9994 39.948]; 

MM = 0; 

for i=1:length(species)
    for j=1:length(Constituents)
        if strcmp(species(i),Constituents(j))
           MM = MM + Atomic_weight(j); 
           u = j;
        end
    end
    n = str2num(species(i));
    if n>1
        MM = MM + Atomic_weight(u)*(n-1);
    end
end
end

EXPLAINATION:

1.) This function takes species_name as input and gives out its Molecular mass as output.

2.) Each species name is stored in a string called species.

3.) From the file given, it can be seen that the constituents that make up each species are Hydrogen(H), Carbon(C), Nitrogen(N), Oxygen(O) and Argon(Ar). So the starting letter of each element is stored as constituents .

4.) The atomic weights of each constituent is stored in an array called atomic_weight.

5.) The molecular weight is set to zero in the beginning,

6.) Using the nested for loop, molecular weight is calculated as follows:

i.) The first for loop runs for entire length of species name that has been determined by the for loop in the main program.

ii.) The second for loop runs for entire length of constituent array.

iii.) Using the strcmp command which compares2strings and returns 1 if they match, the molecular weight will be incremented by the corresponding value in the array\'atomic_weight\'.

iv.) Another if statement checks if a molecule in a specie has more than one molecule and increases its molecular weight correspondingly.

RESULT:

1.)SCreenshot of windows where all the plots are saved:

\"\"

2.) Plots of O2, N2, and CO2 species:

i.) O2 plots:

  • Enthalpy

\"\"

  • Entropy

\"\"

  • Specific Heat

\"\"

ii.) N2 plots:

  • Enthalpy

\"\"

  • Entropy

\"\"

  • Specific heat

\"\"

iii.) CO2 plots:

  • Enthalpy

\"\"

  • Entropy

\"\"

  • Specific heat

\"\"

3.) Screenshot of command window displaying the molecular weight of each species:

\"\"

\"\"

\"\"

\"\"

ERRORS:

The error I faced was that, when I typed in AR for argon in the constituent array, I got an error as shown in the screeshot below:

\"\"

As a remedy, I substituted AR in the constituent array with A.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.