Project

NASA File Parsing

Aim: To write a program in MATLAB

Objective:

  1. To write a fucntion that extracts the 14 co-efficients and calculates the enthalpy,entropy and specific heat of all the species in the data file
  2. To calculate the molecular weight of each species
  3. To plot the enthalpy,entropy and specific heat of all species and save in individual folders

Theory:

File parsing is the proces of extracting data from a given input file and implementing it in the program. Parsing a file means to split the data or other intputs into pieces of data so that it can be easily stored or manipulated. In the given data file, we are splitting the string into parts and then convert them into something simpler than strings.

Here we extract the data from the given thermodynamic data file and calculate enthalpy, entropy and specific heat for all species. We also calculate the molecular weight.

To calculate enthalpy, entropy and specific heat , we use the following equations

where,

T = local temperature

a1-a7 = high temperature co-efficients

a8-a14 = low temperature co-efficients

R = Gas constant = 8.314

Program:

Function for Enthalpy:

function [H] = enthalpy(b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,local_low_temp,local_mid_temp,local_high_temp,t,R)

if (t>local_mid_temp)
    H = (b1+((b2.*t)/2)+((b3.*(t.^2))/3)+((b4.*(t.^3))/4)+((b5.*(t.^4))/5)+(b6./t)).*t*R;
else
    H = (b8+((b9.*t)/2)+((b10.*(t.^2))/3)+((b11.*(t.^3))/4)+((b12.*(t.^4))/5)+(b13./t)).*t*R;
end

Function for Entropy:

function [S] = entropy(b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,local_low_temp,local_mid_temp,local_high_temp,t,R)

if (t>local_mid_temp)
    S = ((b1.*log(t))+(b2.*t)+((b3.*(t.^2))/2)+((b4.*(t.^3))/3)+(b5.*(t.^4))+b7)*R;
else
    S = ((b8.*log(t))+(b9.*t)+((b10.*(t.^2))/2)+((b11.*(t.^3))/3)+(b12.*(t.^4))+b14)*R
end

Function for Specific Heat:

function [c_p] = specific_heat(b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,local_low_temp,local_mid_temp,local_high_temp,t,R)

if(t>local_mid_temp)
    c_p = R*(b1+(b2.*t)+(b3.*(t.^2))+(b4.*(t.^3))+(b5.*(t.^4)));
else
    c_p = R*(b8+(b9.*t)+(b10.*(t.^2))+(b11.*(t.^3))+(b12.*(t.^4)));
end

Function for Molecular weight:

function [M] = molecular_wt(species_name)

atomic_mass = [15.999,1.008,12.011,14.007,39.948];
elements = ['O','H','C','N','A'];

mol_wt=0;
for i=1:length(species_name)
    for j=1:length(elements)
        if strcmpi(species_name(i),elements(j))
            mol_wt = mol_wt+atomic_mass(j);
            z=j;
        end
    end
    t = str2double(species_name(i));
    if(t>1)
        mol_wt = mol_wt+(atomic_mass(z)*(t-1));
    end
end
M = mol_wt
end

Main Program:

clear all
close all
clc

f1 = fopen('THERMO.DAT','r');

%Reading header information
fgetl(f1);

%Global temperature

t_line = fgetl(f1);

a = strsplit(t_line,' ');

global_low_temp = str2double(a{2});
global_mid_temp = str2double(a{3});
global_high_temp = str2double(a{4});

%Neglecting the next 3 lines

fgetl(f1);
fgetl(f1);
fgetl(f1);

%Reading species block
%Line 1

for i = 1:53
    t_line2 = fgetl(f1);
    s = strsplit(t_line2,' ');
    species_name = s{1};
    A = strfind(t_line2,'G');
    temp_values = t_line2(A+4:length(t_line2));
    B =strsplit(temp_values,' ');
    
    local_low_temp = str2double(B{1});
    local_mid_temp = str2double(B{2});
    local_high_temp = str2double(B{3});
    
 t_line = fgetl(f1);
 b = findstr(t_line,'E');
 % 1st co-efficient
 b1 = t_line(1:b(1)+3);
 b1 = str2num(b1);
 % 2nd co-efficient
 b2 = t_line(b(1)+4:b(2)+3);
 b2 = str2num(b2);
 % 3rd co-efficient
 b3 = t_line(b(2)+4:b(3)+3);
 b3 = str2num(b3);
 % 4th co-efficient
 b4 = t_line(b(3)+4:b(4)+3);
 b4 = str2num(b4);
 % 5th co-efficient
 b5 = t_line(b(4)+4:b(5)+3);
 b5 = str2num(b5);
 
 %line 2
 line2 = fgetl(f1);
 % 6th co-efficient
 b6 = line2(1:b(1)+3);
 b6 = str2num(b6);
 % 7th co-efficient
 b7 = line2(b(1)+4:b(2)+3);
 b7 = str2num(b7);
 % 8th co-efficient
 b8 = line2(b(2)+4:b(3)+3);
 b8 = str2num(b8);
 % 9th co-efficient
 b9 = line2(b(3)+4:b(4)+3);
 b9 = str2num(b9);
 % 10th co-efficient
 b10 = line2(b(4)+4:b(5)+3);
 b10 = str2num(b10);
 
 %line 3
 line3 = fgetl(f1);
 % 11th co-efficient
 b11 = line3(1:b(1)+3);
 b11 = str2num(b11);
 % 12th co-efficient
 b12 = line3(b(1)+4:b(2)+3);
 b12 = str2num(b12);
 % 13th co-efficient
 b13 = line3(b(2)+4:b(3)+3);
 b13 = str2num(b13);
 % 14th co-efficient
 b14 = line3(b(3)+4:b(4)+3);
 b14 = str2num(b14);
 
 % Universal gas constant
 R = 8.314;
 
 t = linspace(local_low_temp,local_high_temp,100);
 
 
  
 
 % Specific heat
 c_p = specific_heat(b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,local_low_temp,local_mid_temp,local_high_temp,t,R);
 
 % Enthalpy
 H = enthalpy(b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,local_low_temp,local_mid_temp,local_high_temp,t,R);
 
 % Entropy
 S = entropy(b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,local_low_temp,local_mid_temp,local_high_temp,t,R);
 
 % Making directory
  mkdir(species_name)
  cd(species_name)
  
 %Plots
 
 % Temperature vs Specific Heat
 
 figure(1)
 plot(t,c_p)
 xlabel('Temperature')
 ylabel('Specific Heat')
 title('Temperature vs Specific Heat')

 
 % Temperature vs Enthalpy
 figure(2)
 plot(t,H)
 xlabel('temperature')
 ylabel('enthalpy')
 title('Temperature vs Enthalpy')
 
 
 % Temperature vs Entropy
 figure(3)
 plot(t,S)
 xlabel('Temperature')
 ylabel('Entropy')
 title('Temperature vs Entropy')
 


 saveas(1,'Specific Heat','jpg')
 saveas(2,'Enthalpy','jpg')
 saveas(3,'Entropy','jpg')
 
 cd ..
 
 % Calculating Molecular Mass
 
 
 [M] = molecular_wt(species_name);
 K = sprintf('%s mol_wt=%f',species_name,M);
 disp(K)
end
fclose(f1)
    

Code Explanation:

  1. Open the file 'THERMO.DAT' using the fopen command and extract the data using fgetl command
  2. Use the loop command to get all the co-efficients and temperature from the data file
  3. Convert the string to numbers using the str2num command
  4. Use the if command to create folder for each species and save the data into that individual folder
  5. Plot the outputs for Enthalpy, Entropy and Specific Heat for each species
  6. Calculate and display the molecular weight of each Species

Results:

For N2:

For O2

For CO2:

Folders:

Molecular Weight:

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.