Challenge

Project 1 - Parsing NASA thermodynamic data

Aim :- Write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.

Objective :-

  1. Write a function that extracts the 14 coefficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.
  2. Calculate the molecular weight of each species.
  3. Plot the Cp, Enthalpy, and Entropy specific for each species, and for O2, N2 and CO2.

Formula used :-

Matlab Main code:-

% Nasa file parsing program 
clear all
 close all
 clc
 
% inputs
 R = 8.314; 
 
 f1 = fopen('THERMO.dat','r');
 l1 = fgetl(f1);
 l2 = fgetl(f1);
 t = strsplit(l2);
 global_min = str2double(t{2});
 global_mid = str2double(t{3});
 global_max = str2double(t{4});
 
 temp = linspace(global_min,global_max,400);
 
 for i = 1:3
     fgetl(f1);
 end
 
 for i= 1:53
     line_1 = fgetl(f1);
     A = strsplit(line_1);
     Species_name = (A{1});
     
     tline_2 = fgetl(f1);
     tline_3 = fgetl(f1);
     tline_4 = fgetl(f1);
     
     a = strfind(tline_2,'E');
     b = strfind(tline_3,'E');
     c = strfind(tline_4,'E');
     
     %extraction of high temperature coefficients(a1:a7)
     a1 = str2double(tline_2(1:a(1)+3));
     a2 = str2double(tline_2(a(1)+4:a(2)+3));
     a3 = str2double(tline_2(a(2)+4:a(3)+3));
     a4 = str2double(tline_2(a(3)+4:a(4)+3));
     a5 = str2double(tline_2(a(4)+4:a(5)+3));
     a6 = str2double(tline_3(1:b(1)+3));
     a7 = str2double(tline_3(b(1)+4: b(2)+3));
     
     %extraction of low temperature coefficients (b1:b7)
     b1 = str2double(tline_3(b(2)+4: b(3)+3));
     b2 = str2double(tline_3(b(3)+4: b(4)+3));
     b3 = str2double(tline_3(b(4)+4: b(5)+3));
     b4 = str2double(tline_4(1:c(1)+3));
     b5 = str2double(tline_4(c(1)+4: c(2)+3));
     b6 = str2double(tline_4(c(2)+4: c(3)+3));
     b7 = str2double(tline_4(c(3)+4: c(4)+3));
     
     
  C_p = specific_heat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,R,temp,global_mid);
    h = enthalpy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,R,temp,global_mid);
    S = entropy(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,R,temp,global_mid);

       
% plotting
       figure(1)
       subplot(3,1,1)
       plot(temp, C_p, 'linewidth', 4,'color', 'b');
       title(Species_name)
       xlabel("Temperature")
       ylabel("Specific Heat")
       subplot(3,1,2);
       plot(temp, h, 'linewidth', 4,'color', 'r');
       title(Species_name)
       xlabel("Temperature")
       ylabel("Enthalpy")
       subplot(3,1,3);
       plot(temp, S, 'linewidth', 4,'color', 'g');
       title(Species_name)
       xlabel("Temperature")
       ylabel("Entropy")
       
       saveas(figure1,'Nasa_data','png')
   MW = molecular_weight(Species_name);
      
 end
 
 fclose(f1)

% program end

Function code for entropy

function [S] = entropy(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,R,temp, global_mid)
if temp <= global_mid
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

Function code for enthalpy

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

Function code for specific heat

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

Function code for molecular-weight

function [MW] = molecular_weight(Species_name)

element= {'H' 'C' 'N' 'O' 'Ar'};
atomic_weight = [1 12 14 16 40];

MW = 0;

for i = 1:length(Species_name)
    for j = 1:length(atomic_weight);
        if strcmpi(Species_name(i), element(j));
            MW = MW + atomic_weight(j);
            
            q = j;
        end
    end
    
    m = str2double(Species_name(i));
    if m>1
        MW = MW + (atomic_weight(q)*(m-1));
    end
    
end

fprintf('Molecular weight of %s is %d',Species_name,MW);
disp(' ')
end

Matlab code explanation:-

-Programing language used matlab online

-starting the program using comments

-The value of R is provided as input in the code.

-f1 = fopen to open the file "Thermo.dat" under consideration for the data and using "r" indicating to read the command

-fgetl command is used to detect the first two lines l1 and l2.

-strsplit command is used to split the string by default delimiter and gather the corresponding temperature data points.

-using str2double is an enhanced command by the virtue of the performance criteria.

-The temperature is broken into 400 intervals using the linspace command over the min and max temperatures.

-The for loop is initiated for the 53 constituents mentioned in the data file

-t_line2, t_line3 and t_line4 are important and are assigned using fgetl, these 3 lines contains the 14 coefficients for the determination of the properties.

-strfind command is used to identify the letter E from the strings and is assigned to a,b and c for the corresponding lines.

-The values a1,a2,a3,a4,a5,a6,a7 represents the high temperature coefficients.

-The values b1,b2,b3,b4,b5,b6,b7 represents the low temperature coefficients.

-The strings are converted to values using the str2double and by identifying E in the strings accordingly.

-mkdir command is introduced to make the directory for all the values and plots determined

-C_p, h and s are calculated using them as a separate function using the formulas mentioned initially

-using the plot command to plot for cp ,h ,&s

-ending the for loop

Command window exicution/ Outpur :-

Folder Created in matlab drive :-

Result / Conclusion :-

- thus we have Written a function/ program in matlab that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species

- also calculated the molecular weight for each species and its output is displayed in command window exicution

- also generated plots for c_p , enthalpy & entropy vs temp for O2 , N2 & CO2 . The graphs are shown below

GRAPH OF c_p , enthalpy & entropy vs temp for Oxygen

GRAPH OF c_p , enthalpy & entropy vs temp for Nitrogen

GRAPH OF c_p , enthalpy & entropy vs temp for Carbon-dioxide

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.