Challenge

Project 1 - Parsing NASA thermodynamic data

Parsing Thermodynamic data from NASA using MATLAB

Aim – To parse thermodynamic data from NASA

Objective -

To write a code extracting 14 coefficients of each Species from the data file and calculating Cp, H ,S.

To calculate molecular weight of each SPECIES .

To plot graph for each SPECIES in regards of Temperature vs its thermodynamic properties.

Theory – The data file of NASA consists of thermodynamic values for different temperatures ranges which has fourteen different polynomials for each specific SPECIES. These values are used to find Specific Heat(Cp),Enthalpy(H),Entropy(S) which are then compared with array of values ranging between Higher and Lower Temperature to see the characteristics of each SPECIES .

‘Parsing’ basically means reading a MATLAB ‘.m’ file to read the Thermodynamic data file consisting of values for different SPECIES for different temperature.

Governing Equation

SPECIFIC HEAT

ENTHALPY

ENTROPY

where,

Cp = Specific Heat

H = Enthalpy

S = Entropy

R = Universal Gas Constant = 8.314 J/mol-K

T = Temperature

Program

Main Code

%Parsing thermodynamic properties from NASA data file
clear all
close all
clc

%Reading the data file
f1 = fopen('THERMO.dat','r');
file_name = fgetl(f1);
tline = fgetl(f1);
A = strsplit(tline,' ');

%Defineing the temperatures
global_low_temp = str2double(A{2});
global_mid_temp = str2double(A{3});
global_high_temp = str2double(A{4});

for i= 1:3
    comments = fgetl(f1);
end

for i = 1:53
    
    %Extracting spices name
    species = fgetl(f1);
    species_name = species(1:6);
   
    %Extracting coefficients from first line
    line_1 = fgetl(f1);
    A1 = strfind(line_1,'E');
    a1 = str2double(line_1(1:A1(1)+3));
    a2 = str2double(line_1(A1(1)+4 : A1(2)+3));
    a3 = str2double(line_1(A1(2)+4 : A1(3)+3));
    a4 = str2double(line_1(A1(3)+4 : A1(4)+3));
    a5 = str2double(line_1(A1(4)+4 : A1(5)+3));
    
    %Extracting coefficients from second line
    line_2 = fgetl(f1);
    A2 = strfind(line_2,'E');
    a6 = str2double(line_2(1:A2(1)+3));
    a7 = str2double(line_2(A2(1)+4 : A2(2)+3));
    a8 = str2double(line_2(A2(2)+4 : A2(3)+3));
    a9 = str2double(line_2(A2(3)+4 : A2(4)+3));
    a10 = str2double(line_2(A2(4)+4 : A2(5)+3));
    
    %Extracting coefficients from third line
    line_3 = fgetl(f1);
    A3 = strfind(line_3,'E');
    a11 = str2double(line_3(1:A3(1)+3));
    a12 = str2double(line_3(A3(1)+4 : A3(2)+3));
    a13 = str2double(line_3(A3(2)+4 : A3(3)+3));
    a14 = str2double(line_3(A3(3)+4 : A3(4)+3));
    
    
    %Generating Temperature values
    T = linspace(global_low_temp,global_high_temp,500);
    
    %Calling Thermodynamic eq function
    [cp,h,s] = coeff(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T);
    
    %Calculating molecular weight
    molecular_weight = mole_wt(species_name);
    
    
    %Plotting
    
    %Creating folder Species 
    cd(pwd)
    mkdir(species_name);
    
    %Plotting temp vs cp
    figure(1)
    plot(T,cp,'linewidth',2,'color','r')
    xlabel('Temperature')
    ylabel('Specific Heat')
    title(species_name)
    
    %Plotting temp vs h
    figure(2)
    plot(T,h,'linewidth',2,'color','b')
    xlabel('Temperature')
    ylabel('Enthalpy')
    title(species_name)
    
    %Plotting temp vs s
    figure(3)
    plot(T,s,'linewidth',2,'color','g')
    xlabel('Temperature')
    ylabel('Entropy')
    title(species_name)
   
    cd(species_name)
    
    %Saving Plots
    saveas(1,'specific_heat.png')
    saveas(2,'enthalpy.png')
    saveas(3,'entropy.png')
    
    cd ..
end 

Function Code

Coeff file

function [cp,h,s] = coeff(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T)

%Universal gas constant [J/mol-K]
R = 8.314;

for i = 1:length(T)
   if T(i) < 300
       
       cp = (a1 + a2.*T + a3.*T.^2 + a4.*T.^3 + a5.*T.^4).*R;
       h = (a1 + a2.*(T./2) + a3.*(T.^2./3) + a4.*(T.^3./4) + a5.*(T.^4./5) + a6./T).*(R*T);
       s = (a1.*log(T) + a2.*T + a3.*(T.^2./2) + a4.*(T.^3./3) + a5.*(T.^4./4) + a7).*R;
       
   else
       
       cp = (a8 + a9.*T + a10.*T.^2 + a11.*T.^3 + a12.*T.^4).*R;
       h = (a8 + a9.*(T./2) + a10.*(T.^2./3) + a11.*(T.^3./4) + a12.*(T.^4./5) + a13./T).*(R*T);
       s = (a8.*log(T) + a9.*T + a10.*(T.^2./2) + a11.*(T.^3./3) + a12.*(T.^4./4) + a14).*R;
   end
end
end

Molecular Weight file

function[molecular_weight] = mole_wt(species_name)

Species = upper(species_name);
constituents = ['H','C','N','O','A'];
atomic_weight = [1 12 14 16 40];

molecular_weight = 0;
for i = 1:length(Species)
    for j = 1:length(constituents)
    if strcmp(Species(i),constituents(j))
        molecular_weight = molecular_weight + atomic_weight(j);
        u = j;
    end
    end


n = str2double(Species(i));
if n > 1
    molecular_weight = molecular_weight + atomic_weight(u)*(n-1);
end
end

fprintf('molecular_weight of %s:- ',species_name);
fprintf('%d',molecular_weight);
fprintf('amu');
fprintf('\n');
end

Explanation

Main code consists of first reading the thermodynamic data from the data file and assigning the values to variables as in second line of data file temperature values are given and they are assigned as low mid and high temperature values.

Then ‘for loop’ is created to read 53 different SPECIES one by one which consists of 14 different values which are assigned to certain variables to calculate Specific Heat(Cp),Enthalpy(H) and Entropy(S) for that particular Species.

Function file named ‘coeff’ is created to calculate Cp, H and S using formula which uses that 14 different values, which is then called in main program after the coefficients values are assigned .

Function file named ‘mole_wt ‘ is created to calculate molecular weight of each SPECIES in which array of elements that are the base elements of the SPECIES are created and named constituents and atomic weight of that elements are mentioned in the file.

Then using two conditions the molecular weight are calculated in which the first condition consists of comparision of the SPECIES and the constituents if they are same the molecular weight will be same i.e. the constituents consists of ‘O’ element and it is compared to ‘O’ in data file the molecular weight will directly be the atomic weight of that element, if it is ‘O2’ second condition comes into action to calculate its moelecular weight. Same goes with other elements.

Then plotting is done for those SPECIES which consists of three plots which are Specific Heat(Cp), Enthalpy(H),Entropy(S) Vs 500 values of Temperature between lower and higher temperature.

Using ‘mkdir’ command, folders are created with species name in the folder the codes are written and saved.

Results

Folder created for each Species

Molecular Weight in Command Window

Plotting Results

For O2

Temperature(T) vs Specific Heat(Cp)

Temperature(T) vs Enthalpy(H)

Temperature(T) vs Entropy(S)

For N2

Temperature(T) vs Specific Heat(Cp)

Temperature(T) vs Enthalpy(H)

Temperature(T) vs Entropy(S)

For CO2

Temperature(T) vs Specific Heat(Cp)

Temperature(T) vs Enthalpy(H)

Temperature(T) vs Entropy(S)

Errors

The problem was the results were saved only in 'O' folder and the plot of other Species were also saved in one folder in one.

Problem was solved later by correctly givving the path file.

Conclusion

Parsing of thermodynamic data file is done in MATLAB.

Specific Heat(Cp),Enthalpy(H),Entropy(S) and molecular weight are calculated using function file.

The plots are saved in the folder created by us.

These shows its characteristics change with respect to Temperature .

References

Mathworks library

Youtube Videos

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.