Challenge

Project 1 - Parsing NASA thermodynamic data

AIM:

To access the given NASA thermodynamic data using file parsing to find out the enthalpy, entropy and the specific heat of 53 different elements, plot the very same against local temperature range and save the plots obtained in folders with appropriate names.

INTRODUCTION:

NASA created polynomials that can be used to calculate the thermodynamic properties of different elements. They have also documented the co-efficients that are required to evaluate these polynomials for 1000+ species.

They can be done so, by the help of the following formula:

Where, R is the universal gas constant and the only input needed.

THEORY:

File parsing is a very important tool in matab as it help in both creating, reading and writing data from a file while working. It can be simply done by fopen function and in combination with other functions, a lot of tasks can be completed.

MAIN CODE:

clear all
close all
clc

% universal gas constant as input
R= 8.314;

% reading the data file by setting it in read mode
file = fopen('THERMO.dat','r');

% accessinng the first line of the file by using the fgetl function
l1 =fgetl(file);

% getting access to the temperature values stored in the second line
l2=fgetl(file);
temp_range = strsplit(l2,' ');
global_min = str2num(temp_range{2});
global_mid = str2num(temp_range{3});
global_max = str2num(temp_range{4});

%Defining the temperature range
temp = linspace (global_min,global_max,500);

% skipping the three comment lines 
for i=1:3
fgetl(file);
end

% loop to access the data of 53 elements
for i=1:53
    % reading first line for species information
    l=fgetl(file);
    % accessing the name of the species
    w = strsplit(l,' ');
    element_name = (w{1});
    % accessing the coefficients line that contains values of a1 to a7

    l1=fgetl(file);
    l2=fgetl(file);
    l3=fgetl(file);
    
    % searchiing the position of "E" to distinguish each value
    a=strfind(l1,'E');
    b=strfind(l2,'E');
    c=strfind(l3,'E');
    
    % extracting high temp coeff(the first 7 values)
    a1=str2num(l1(1:a(1)+3));
    a2=str2num(l1(a(1)+3:a(2)+3));
    a3=str2num(l1(a(2)+4:a(3)+3));
    a4=str2num(l1(a(3)+4:a(4)+3));
    a5=str2num(l1(a(4)+4:a(5)+3));
    a6=str2num(l2(1:b(1)+3));
    a7=str2num(l2(b(1)+4:b(2)+3));

    % extracting low temp coeff(the second 7 values)
    b1=str2num(l2(b(2)+4:b(3)+3));
    b2=str2num(l2(b(3)+4:b(4)+3));
    b3=str2num(l2(b(4)+4:b(5)+3));
    b4=str2num(l3(1:c(1)+3));
    b5=str2num(l3(c(1)+4:c(2)+3));
    b6=str2num(l3(c(2)+4:c(3)+3));
    b7=str2num(l3(c(3)+4:c(4)+3));
    
    % calculating the required parameters by calling out the respective
    % functions
    
   c_p = specific_heat(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,temp,global_min,global_max,global_mid);
   
    H = enthalpy(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,temp,global_min,global_max,global_mid);

    S = entropy(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,temp,global_min,global_max,global_mid);

    % calling function to plot
    plot_func(c_p,H,S,temp,element_name)
    
    %molar weight calculation
    mol_wt(i) = molecular_weight(element_name)

end
fclose (file)



FUNCTIONS:

SPECIFIC HEAT:

function c_p = specific_heat(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,temp,global_min,global_max,global_mid)
  if temp<=global_mid 
      c_p = R*(a1 + a2.*temp + a3.*(temp.^2) + a4.*(temp.^3) + a5.*(temp.^4))
  else
        c_p = R*(b1 + b2.*temp+b3.*(temp.^2) + b4.*(temp.^3) + b5.*(temp.^4))
  end

ENTHALPY:

function H = enthalpy(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,temp,global_min,global_max,global_mid)
  if temp <=global_mid
        H = R.*temp.*(a1 + a2.*(temp./2) + a3.*((temp.^2)./3) + a4.*((temp.^3)./4) + a5.*((temp.^4)./5) + a6./temp)
    else

        H = R.*temp.*(b1 + b2.*(temp./2) + b3.*((temp.^2)./3) + b4.*((temp.^3)./4) + b5.*((temp.^4)./5) + b6./temp)

end 

ENTROPY:

function S = entropy(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,temp,global_min,global_max,global_mid)

    if temp<=global_mid

        S = R*(a1.*log(temp) + a2.*temp + a3.*(temp.^2)./2 + a4.*(temp.^3)./3 + a5.*(temp.^4)./4 + a7)

   else

        S = R*(b1.*log(temp) + b2.*temp + b3.*(temp.^2)./2 + b4.*(temp.^3)./3 + b5.*(temp.^4)./4 + b7)

 end

MOLECULAR WEIGHTS:

function [w] = molecular_weight(element_name) 
s = {'H' 'C' 'N' 'O' 'AR'};
%atomic weight of each elements
atomic_wt = [1 12 14 16 40];                                                      
w = 0;                                                                        
for i = 1:length(element_name) 
for j = 1:length(s)   
if strcmp(element_name(i), s(j))   
 w= w + atomic_wt(j);     
%storing the position of matched element   
position = j;                                                           
end
end    
n = str2num(element_name(i));
if n > 1                                                     
w = w + atomic_wt(position)*(n-1); 
end
end
end

FUNCTION FOR PLOTTING AND SAVING GRAPHS:

function plot_func(c_p,H,S,temp,element_name)

cd(pwd)
dir_name=element_name;
mkdir(dir_name);

figure(1) 
plot(temp,c_p,'lineWidth',3)
grid on
title(sprintf('Specific Heat vs Temperature for %s:',element_name))
xlabel('Temperature')
ylabel('Specific Heat') 

figure(2)
plot(temp,H,'linewidth',3)
grid on
title(sprintf('Enthalpy vs Temperature for %s:',element_name))
xlabel('Temperature')
ylabel('Enthalpy') 

figure(3)
plot(temp,S,'linewidth',3)
grid on
title(sprintf('Entropy vs Temperature for %s:',element_name))
xlabel('Temperature')
ylabel('Entropy') 
cd(dir_name)
saveas(1,'c_p_vs_temp.png')
saveas(2,'H_vs_temp.png')
saveas(3,'S_vs_temp.png') 
cd .. 
end

OUTPUTS:

PLOTS OF O2:

PLOTS FOR N2:

PLOTS FOR CO2:

SCREENSHOT OF FOLDERS:

REPORT:

The first step to carry was to open the file itself. It was done using the fopen command and setting the mode as 'r' (read mode).

Then, to access the content inside the file, we can use the fgetl command to read one line of the file at a time, and also store it in any variable if needed.

We had to then access the values of the second line of the file that were in the form of a string. So, we used the strsplit function with splitter as ' ' to store each character divided by a space bar and store it in a cell. And finally, use the str2num function to convert the string value stored, into an integer and stored the values in appropriate variables.

Then we skipped the hree comment lines by using getfl three times.

Now, we had to access the source files. Since the infor mation for the source files occured in a certain manner, repeated after every 4 lines, it was possible to extract the files. We used similar method to extract the element name from the first line of the species block by using strsplit.

Then, to access the values, we used the strfind function to locate the position of 'E' first and then used it to get the complete values of the variables, as only 3 elements occured after every 'E'. Hence, by repeating this logic, we were able to locate every variable in the species block.

The data was stored in variables. And then, certain functions were called out to find the required values and then, the plot function was created to plot and store the plots in certain folders.

All the functions were called inside a for loop to repeat the process 53 times for each and every element. The plotting function was created by plotting the given three graphs and then use the saveas command to save the current figures in the required folder name.

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.