Challenge

Project - Parsing NASA thermodynamic data

Specific Heat (J/Kg K)

It is the quantity of heat required to raise the temperature of one gram of a substance by one Celsius degree. The units of specific heat are usually calories or joules per gram per Celsius degree.

k

Enthalpy (J)

It is the sum of the system's internal energy and the product of its pressure and volume. It is a state function used in many measurements in chemical, biological, and physical systems at a constant pressure, which is conveniently provided by the large ambient atmosphere.

Entropy (J/K)

It is the measure of a system’s thermal energy per unit temperature that is unavailable for doing useful work. Because work is obtained from ordered molecular motion, the amount of entropy is also a measure of the molecular disorder, or randomness, of a system.

Function Codes And Their Explaination For Finding Specific Heat (J/Kg K), Entropy (J/K), Enthalpy (J), and Molecular W

eight

1. Specific Heat (J/Kg K)

Code

function Cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,local_temp_range, local_medium_temp, R)
            if local_temp_range <= local_medium_temp
                Cp = R*(a8 +(a9.*local_temp_range)+(a10.*local_temp_range.^2)+(a11.*local_temp_range.^3)+(a12.*local_temp_range.^4));              
            else
                Cp = R*(a1 +(a2.*local_temp_range)+(a3.*local_temp_range.^2)+(a4.*local_temp_range.^3)+(a5.*local_temp_range.^4));
            end
end

Explaination

Here, we replicate the equation for Specific Heat in matlab code. This code evaluates Specific Heat for obtained local temperature range and coefficients. Using a if-else condition, coefficients of low and high temp range is used as required.

2. Enthalpy (J)

Code

function H = enthalpy (a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,local_temp_range, local_medium_temp, R)
             if local_temp_range <= local_medium_temp
                 H = R*local_temp_range.*(a8 + ((a9*local_temp_range)/2) + ((a10*(local_temp_range).^2)/3) + ((a11*(local_temp_range).^3)/4) + ((a12*(local_temp_range).^4)/5) + (a13./local_temp_range));                 
             else
                 H = R*local_temp_range.*(a1 + ((a2*local_temp_range)/2) + ((a3*(local_temp_range).^2)/3) + ((a4*(local_temp_range).^3)/4) + ((a5*(local_temp_range).^4)/5) + (a6./local_temp_range));
end

Explaination

Here, we replicate the equation for enthaply in matlab code. This code evaluates enthalpy for obtained local temperature range and coefficients. Using a if-else condition, coefficients of low and high temp range is used as required.

3. Entropy (J/K)

Code

function S = entropy (a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,local_temp_range,R,local_medium_temp)
             if local_temp_range <= local_medium_temp
                 S = R*(a8.*log(local_temp_range)+(a9.*local_temp_range)+(a10*((local_temp_range).^2)/2)+(a11*((local_temp_range).^3)/3)+(a12*((local_temp_range).^4)/4)+a14);
             else
                 S = R*(a1.*log(local_temp_range)+(a2.*local_temp_range)+(a3*((local_temp_range).^2)/2)+(a4*((local_temp_range).^3)/3)+(a5*((local_temp_range).^4)/4)+a7);
             end
end

Explaination

Here, we replicate the equation for entropy in matlab code. This code evaluates entropy for obtained local temperature range and coefficients. Using a if-else condition, coefficients of low and high temp range is used as required.

4. Molecular Weight

Code

function mol_weight = molecular_weight(Species_Name)
                      atomic_names = ['H','C','O','N','Ar']; %symbols of elements
                      atomic_weights = [1.008 12.011 15.999 14.007 39.948]; %atomic weights of above mentioned elements
                      mol_weight = 0;
                      for i =  1:length(Species_Name)
                          for j = 1:length(atomic_names)
                              if strcmp(Species_Name(i), atomic_names(j)) %compares both strings and if equal calculated atomic weight
                                  mol_weight = mol_weight + atomic_weights(j);
                                  pos = j;
                              end
                          end
                          n = str2num(Species_Name(i)); %if species has more than 1 element, the next piece of code calculates and adds the weight of the other elements
                          if n>1
                              mol_weight = mol_weight + atomic_weights(pos)*(n-1);
                          end                        
                      end
                     fprintf('n Molecular Weight of %s is %f u n',Species_Name,mol_weight) % prints mol_weight in command window
end

Explaination

  • First we find all unique elements by going through the data file and we find them to be H, C, O, N, and Ar. Now, we find their respective atomic weights and enter them manually into the function.
  • We now take each Species_Name and then compare it with each atomic_names using if condition, strcmp and for loops and calculate the molecular weight.
  • Now, some species may have more than 1 element, to find this and add the same to molecular weight, we change the species name from str to num using 'str2num' and if it has more than 1 element, we add the atomic weights again to calculate the molecular weight.
  • Then, we print the molecular weight into the command window.

Main Code

clear all
close all
clc

%reading the file
f1 = fopen('THERMO.dat', 'r');
gt=fgetl(f1);%extracting 1st line

%Gas Constant R
R = 8.314;

% Obtaing Temperatures - by Reading Header Information
global_temperature=fgetl(f1); %extracting 2nd line

% Spliting
t=strsplit(global_temperature,' '); %splitting ' ' and strings in 2nd line 

% Converting string cells to numbers using str2num,
global_low_temp=str2num(t{2}); %2, 3, and 4 is used as 1st str is just a space ' '
global_medium_temp=str2num(t{3});
global_high_temp=str2num(t{4});

global_temperature_range = linspace(global_low_temp, global_high_temp, 1500); % Creating global temp range

gt=fgetl(f1);
gt=fgetl(f1); %comment lines 1-3 in header
gt=fgetl(f1);

for i=1:53
    tline=fgetl(f1);
    x = strsplit(tline,' ');
    Species_Name= (x{1}); %extracting species name
    %extracting local temperatures
    local_low_temp = str2num(x{end-3});
    local_medium_temp = str2num(x{end-1});
    local_high_temp = str2num(x{end-2});

    local_temp_range = linspace(local_low_temp, local_high_temp, 1500); %creating local temp range

    %extracting 1st seven coefficients which are high temp coefficients
    tline1 = fgetl(f1);%line 1 of coefficients.
    x = strfind(tline1,'E'); 
    tline2 = fgetl(f1);%line 2 of coefficients.
    y = strfind(tline2,'E'); 
    tline3 = fgetl(f1);%line 3 of coefficients.
    z = strfind(tline3,'E');
    %find E in the coefficients which helps in sorting
    %with help of variable x,y,and z which has information regarding str 'E' in it,
    %we can start sorting the coefficients
    a1 = tline1(1:x(1)+3);
    a1 = str2num(a1);
    a2 = tline1(x(1)+4:x(2)+3);
    a2 = str2num(a2);
    a3 = tline1(x(2)+4:x(3)+3);
    a3 = str2num(a3);
    a4 = tline1(x(3)+4:x(4)+3);
    a4 = str2num(a4);    
    a5 = tline1(x(4)+4:x(5)+3);
    a5 = str2num(a5);

    %moving to line 2 of coefficients
    a6 = tline2(1:y(1)+3);
    a6 = str2num(a6);
    a7 = tline2(y(1)+4:y(2)+3);
    a7 = str2num(a7);

    %extracting other seven coefficients which are low temp coefficients
    a8 = tline2(y(2)+4:y(3)+3);
    a8 = str2num(a8);
    a9 = tline2(y(3)+4:y(4)+3);
    a9 = str2num(a9);
    a10 = tline2(y(4)+4:y(5)+3);
    a10 = str2num(a10);

    %moving to line 3 of coefficients
    a11 = tline3(1:z(1)+3);
    a11 = str2num(a11);
    a12 = tline3(z(1)+4:z(2)+3);
    a12 = str2num(a12);
    a13 = tline3(z(2)+4:z(3)+3);
    a13 = str2num(a13);
    a14 = tline3(z(3)+4:z(4)+3);
    a14 = str2num(a14);

    % Specific heat calculation using function
    Cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,local_temp_range,R,local_medium_temp);
 
    % Enthalpy calculation using function
    H = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,local_temp_range,R,local_medium_temp);
 
    % Entropy calculation using function
    S = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,local_temp_range,R,local_medium_temp);

    % Calculating molecular weight of species
    mol_weight = molecular_weight(Species_Name);

    %creating and saving into a new folder
    cd('C:UsersnischDesktopMatlab for mechanical engineersProject 1')
    mkdir('Species');
    cd('C:UsersnischDesktopMatlab for mechanical engineersProject 1Species');
    mkdir(Species_Name);
    cd(Species_Name);

    %plot 1 - Cp vs T
    figure(1)
    plot(local_temp_range, Cp, 'Color','blue', 'LineWidth', 3)
    xlabel('Temperature  (K)');
    ylabel('Specific Heat (J/Kg K)');
    title('Specific Heat (Cp) v/s Temperature (T) of',Species_Name);
    saveas(figure(1),'Specific_Heat_Cp.jpg'); %saving figure in the specified directory
   
    %plot 2 - H vs T
    figure(2)
    plot(local_temp_range, H, 'Color','Red', 'LineWidth', 3)
    xlabel('Temperature  (K)');
    ylabel('Enthalpy (J)');
    title('Enthalpy (H) v/s Temperature (T) of',Species_Name);
    saveas(figure(2),'Enthalpy_H.jpg'); %saving figure in the specified directory
   
    %plot 3 - S vs T
    figure(3)
    plot(local_temp_range, S, 'Color','Black', 'LineWidth', 3)
    xlabel('Temperature  (K)');
    ylabel('Entropy (J/K)');
    title('Entropy (S) v/s Temperature (T) of',Species_Name);
    saveas(figure(3),'Entropy_S.jpg'); %saving figure in the specified directory

    cd('C:UsersnischDesktopMatlab for mechanical engineersmatlabfiles'); %changing path to original path

end

Explaination

  • 'fopen' is used to open the given data file and 'fgetl' is used to obtain data line by line.
  • Gas Constant is universal as is predefined in the code as R = 8.314.
  • Using temporary variables, 'strsplit', and 'str2num', global temperatures are extracted and stored.
  • We use 'fgetl' to skip irrelevent lines.
  • A for loop is started. In the for loop, we extract the local temperatures and create a temperature range of 1500 values using linspace.
  • Then using temporary variables, lines containing the coefficient values are extracted.
  • Then using 'strfind' and indexing operations, all the coeffients are stored in their respective variables.
  • Specific heat, Enthaply, Entropy and Molecular weight are all calculated by calling their rescpective functions and then are stored accordingly.
  • The directory is changed and new folder/directories are created as required using 'cd' and 'mkdir' commands.
  • Cp vs T, H vs T, and S vs T plots are created as new figures using 'figure' and 'plot' commands and saved as .jpg files using 'saveas' command.
  • The directory is then changed to original path and the for loop is closed using 'end'

Errors faced

  • Dot product/multiplication was not used initially resulting is undesirable results/errors which was fixed accordingly.
  • Local medium temperature values were used instead of low to high temp rang values in functions which gave empty plots. This was identified and troubleshooted immediately.

Results

1. Folder Screenshot containing all species

2. Plots

2.1 Plots for O2

Plot 1 - Cp vs T
Plot 2 - H vs T
Plot 3 - S vs T

2.2 Plots for N2

Plot 1 - Cp vs T
Plot 2 - H vs T
Plot 3 - S vs T

2.3 Plots for CO2

Plot 1 - Cp vs T
Plot 2 - H vs T
Plot 3 - S vs T

3. Molecular Weight Screenshots (from command window)

References

1. https://www.britannica.com/science/specific-heat

2. https://www.britannica.com/science/entropy-physics

3. https://en.wikipedia.org/wiki/Enthalpy

4. https://in.mathworks.com/help/

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.