Challenge

Project 1 - Parsing NASA thermodynamic data

Aim: To learn file parsing using MATLAB.

Objective:

  • To write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the NASA THERMO data file.
  • Calculate the molecular weight of each species.
  • Plot the Cp, Enthalpy and Entropy for the local temperature range specific for each species.

Governing Equations:

Specific heat:

`C_p/R = a_1 +a_2T+a_3T^2+a_4T^3+a_5T^4`

Enthalpy:

`H/RT=a_1 +a_2T+a_3T^2+a_4T^3+a_5T^4+a_6/T`

Entropy:

`S/R = a_1 lnT +a_2T+a_3T^2/2+a_4T^3/3+a_5T^4/4+a_7`

Body:

Part 1(Program):

Program for File parsing:

clear all
close all
clc

%Reading the header from Thermo data file
f1 = fopen('THERMO.dat','r');
line1 = fgetl(f1);
disp(line1);
line2 = fgetl(f1);
disp(line2);
Header = strsplit(line2,' ');
R = 8.314;
global_low_temp = str2num(Header{2})
global_mid_temp = str2num(Header{3})
global_high_temp = str2num(Header{4})
for i = 1:3
  info_line =  fgetl(f1);
  disp(info_line)
end
%Reading the species block from THERMO data file

for i = 1:53
    Line1 = fgetl(f1);
    species_desc = strsplit(Line1,' ');
    species_name = species_desc{1};
    
    a= flip(species_desc);
    
    local_low_temp = str2num(a{4});
    local_high_temp = str2num(a{3});
    local_mid_temp = str2num(a{2});
    
    temp = linspace(local_low_temp, local_high_temp,500);
    
    Line2 = fgetl(f1);
    a = findstr(Line2, 'E');
     Line3 = fgetl(f1);
    b = findstr(Line3, 'E');
     Line4 = fgetl(f1);
    c = findstr(Line4, 'E');
    
    %High temperature coefficients 
    a1 = str2double(Line2(1:a(1)+3));
    a2 = str2double(Line2(a(1)+4:a(2)+3));
    a3 = str2double(Line2(a(2)+4:a(3)+3));
    a4 = str2double(Line2(a(3)+4:a(4)+3));
    a5 = str2double(Line2(a(4)+4:a(5)+3));
    a6 = str2double(Line3(1:b(1)+3));
    a7 = str2double(Line3(b(1)+4:b(2)+3));
    
    %Low temeprature coefficients
    b1 = str2double(Line3(b(2)+4:b(3)+3));
    b2 = str2double(Line3(b(3)+4:b(4)+3));
    b3 = str2double(Line3(b(4)+4:b(5)+3));
    b4 = str2double(Line4(1:c(1)+3));
    b5 = str2double(Line4(c(1)+4:c(2)+3));
    b6 = str2double(Line4(c(2)+4:c(3)+3));
    b7 = str2double(Line4(c(3)+4:c(4)+3));
    
    specific_heat =  sp_heat_file_pa(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,temp,local_mid_temp);
    enthalpy = enthalpy_file_pa(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,R,temp,local_mid_temp);
    entropy = entropy_file_pa(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,R,temp,local_mid_temp);
    Mw = molecular_weight_file_pa(species_name);
    
    figure(1)
    plot(temp,enthalpy,'linewidth',2,'color','b')
    title('temp vs enthalpy',species_name)
    xlabel('temperature')
    ylabel('Enthalpy')
    grid on
    
    
    figure(2)
    plot(temp,entropy,'linewidth',2, 'color','r')
    title('temp vs entropy',species_name)
    xlabel('temp')
    ylabel('Entropy')
    grid on
    
    
    
    figure(3)
    plot(temp,specific_heat,'linewidth',2,'color','y')
    title('temp vs specific heat',species_name)
    xlabel('temp')
    ylabel('specific heat')
    grid on
    
    mkdir(species_name);
    %directory=(['D:Skill lync', species_name]);
    cd(species_name);
     
    saveas(1,'enthalpy.jpg')
    saveas(2,'entropy.jpg')
    saveas(3,'specific_heat.jpg')
    cd  ..
 disp(['molecular weight of ',species_name]);
 disp(Mw)
end

fclose(f1)

Program for Enthalpy function:

function[h] = enthalpy_file_pa(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,R,temp,local_mid_temp)
if temp <= local_mid_temp
    h = R*temp.*(b1+ (b2.*(temp./2))+ (b3.*(((temp).^2)./3))+ (b4.*(((temp).^3)./4))+ (b5.*(((temp).^4)./5)) + b6./temp);
else
   h = R*temp.*(a1+ (a2.*(temp./2))+ (a3.*(((temp).^2)./3))+ (a4.*(((temp).^3)./4))+ (a5.*(((temp).^4)./5)) + a6./temp);     
end
end

Program for Entropy Function:

function[s] = entropy_file_pa(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,R,temp,local_mid_temp)
if temp<= local_mid_temp
    s = R.*(b1.*log(temp) + (b2.*temp) + (b3.*(((temp).^2)./2)) + (b4.*(((temp).^3)./3)) + (b5.*(((temp).^4)./4)) + b7);
else
    s = R.*(a1.*log(temp) + (a2.*temp) + (a3.*(((temp).^2)./2)) + (a4.*(((temp).^3)./3)) + (a5.*(((temp).^4)./4)) + a7);
end
end

Program for Specific Heat Function:

function[c_p] = sp_heat_file_pa(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,temp,local_mid_temp)
if temp <= local_mid_temp
    c_p = (b1+ (b2.*temp)+ (b3.*((temp).^2))+ (b4.*((temp).^3))+ (b5.*((temp).^4)));
else
    c_p = (a1+ (a2.*temp)+ (a3.*((temp).^2))+ (a4.*((temp).^3))+ (a5.*((temp).^4)));
end
end

Program for Molecular Weight Calculation:

function[Mw] = molecular_weight_file_pa(species_name)

elements = ['H', 'C', 'N', 'O', 'A'];
atomic_weight = [1, 12, 14, 16, 40];

Mw = 0;
for i = 1:length(species_name)
    for j = 1:length(elements)
       if strcmp(species_name(i), elements(j)) 
        Mw = Mw + atomic_weight(j);
        a = j;
       end
    end

n = str2double(species_name(i));
if n>1
    Mw = Mw+ atomic_weight(a)*(n-1);
end
end
end

Part 2 (Outputs):

Output for molecular weight:

Output for plots:

Temp vs Enthalpy

Temp vs Entropy

Temp vs Specific heat

Screenshots:

Part 3 (Explanation):

File Parsing:

File parsing is used when dealing with vast amount of data. Furthermore, as the data accumulation increases, so does the need to read and understand it. This is where data parsers come into the picture.

Parsing data can help turn unstructured data or unreadable data into more readable and valuable information.

mkdir command:

  • mkdir folderName creates the folder folderName. If folderName exists, MATLAB® issues a warning. If the operation is not successful, mkdir throws an error to the Command Window.
  • mkdir parentFolder folderName creates folderName in parentFolder. If parentFolder does not exist, MATLAB attempts to create it..
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.