Project

Parsing NASA thermodynamic data

Aim- To write a Matlab program for parsing NASA thermodynamic data like enthalpy(H), entropy(Cp) and specific heats (S).

OBJECTIVES-

1. To 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.

2. To calculate the molecular weight of each species and display it in the command window.

3. To plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.

4. To save the plots as images with appropriate names and dump them in separate folders for each species with suitable name. All these should be generated automatically.

GOVERNING EQUATIONS-

Where,

Cp = Specific Heat(J/mol-K)

H = Enthalpy(J/kg)

S = Entropy(J/K)

T = Temperature(K)

R = Gas constant(J/molK)

a1,a2,a3,a4,a5,a6,a7 are the numerical coefficient

File Link-https://drive.google.com/file/d/1AikCHDlz9s_fu8whE81qee_GD4AyYpCP/view

Software used-MATLABR2018a

Matlab Program-

Main program

%parsing NASA'S Thermodynamic data 
clear all
close all
clc

f1 = fopen('THERMO.dat','r');       %opening the THERMO file
file_name = fgetl(f1);              %getting the file name from the first line 
tline_1 = fgetl(f1);                %getting the file value from the second line  
A = strsplit(tline_1,' ');          %splitting the string values

%getting the value of Global temperature
global_low_temp = str2num(A{2});
global_mid_temp = str2num(A{3});
global_high_temp = str2num(A{4});

%getting the comments from thermo file
for i = 1:3
    Comments = fgetl(f1)
end

for j = 1:53
    %getting the species name
    tline_2 = fgetl(f1);
    s = strsplit(tline_2,' ');       %splitting the string values
    species_name = s{1};
    A=strfind(tline_2,'G ');
    temp_values=tline_2(A+4:length(tline_2));
    B=strsplit(temp_values,' ');
    
    %Getting the value of local temperature of different species
    local_low_temp=str2double(B{1});
    local_high_temp=str2double(B{2});
    local_mid_temp=str2double(B{3});
 

    %Extraction of coefficient from first line
    line_1 = fgetl(f1);
    l1 = findstr(line_1,'E');
    a1 = str2num(line_1(1:(l1(1)+3)));
    a2 = str2num(line_1((l1(1)+4:l1(2)+3)));
    a3 = str2num(line_1((l1(2)+4:l1(3)+3)));
    a4 = str2num(line_1((l1(3)+4:l1(4)+3)));
    a5 = str2num(line_1((l1(4)+4:l1(5)+3)));
    
    %Extraction of coefficient from second line
    line_2 = fgetl(f1);
    l2 =  findstr(line_2,'E');
    a6 =  str2num(line_2(1:(l2(1)+3)));
    a7 =  str2num(line_2((l2(1)+4:l2(2)+3)));
    a8 =  str2num(line_2((l2(2)+4:l2(3)+3)));
    a9 =  str2num(line_2((l2(3)+4:l2(4)+3)));
    a10 = str2num(line_2((l2(4)+4:l2(5)+3)));
    
    %Extraction coefficient from third line
    line_3 = fgetl(f1);
    l3 =  findstr(line_3,'E');
    a11 = str2num(line_3(1:(l3(1)+3)));
    a12 = str2num(line_3((l3(1)+4:l3(2)+3)));
    a13 = str2num(line_3((l3(2)+4:l3(3)+3)));
    a14 = str2num(line_3((l3(3)+4:l3(4)+3)));
    
    %creating a folder for species
    Spe_dir=pwd;
    mkdir(species_name);
    cd(species_name);  
    
    T = linspace(local_low_temp,local_high_temp,100);
    mt = local_mid_temp;
    
    % calling the thermodynamic equations function 'coefficient'
    [Cp H S] = coefficient(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,mt);
    
    %Plotting the curve
    %Curve between Temperature and Specific Heat
    figure(1)
    plot(T,Cp,'linewidth',3,'color','r');
    title(species_name);
    xlabel('Temperature[K]');
    ylabel('Specific Heat[J/mol-K]');
    grid on   
    
    %Curve between Temperature and Enthalpy
    figure(2)
    plot(T,H,'linewidth',3,'color','b');  
    title(species_name);
    xlabel('Temperature[K]');
    ylabel('Enthalpy[J/kg]');
    grid on
    
    %Curve between Temperature and Entropy
    figure(3)
    plot(T,S,'linewidth',3,'color','g');    
    title(species_name);
    xlabel('Temperature[K]');
    ylabel('Entropy[J/K]');
    grid on
   
    %Saving the plot in jpg format
    saveas(figure(1),'Specific Heat.jpg');
    saveas(figure(2),'Enthalpy.jpg');
    saveas(figure(3),'Entropy.jpg');
    cd(Spe_dir);

%Calculating Molecular Mass
elements=['H','C','O','N','A'];
atomic_weight=[1 12 16 14 40];
mol_mass=0;
for l=1:length(species_name)
    for m=1:length(atomic_weight)
        if strcmp(species_name(l),elements(m))
            mol_mass=mol_mass+atomic_weight(m);
            position=m;
end
end
 
n=str2double(species_name(l));
if n>1
    mol_mass=mol_mass+atomic_weight(position)*(n-1);
    
end
end
%printing the molecular weight of differet species in command window
fprintf('molecular_weight of %s:- ',species_name)
fprintf('%d amu n',mol_mass)

    
end

fclose(f1)

Function program-

%Function program for calculating specific heat,enthalpy and entropy
function [Cp H S] = coefficient(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,mt)   
R = 8.314;               
for k = 1:length(T)
        if T(k)>mt;
            %Specific heat 
            Cp = (a1 + a2.*T + a3.*T.^2 + a4.*T.^3 + a5.*T.^4).*R;
            %Enthalpy
            H = (a1 + a2.*T./2 + a3.*T.^2./3 + a4.*T.^3./4 + a5.*T.^4./5 + a6./T).*R.*T;
            %Entropy
            S = (a1.*log(T) + a2.*T + a3.*T.^2./2 + a4.*T.^3./3 + a5.*T.^4./4 +a7).*R;
        else
            %Specific heat 
            Cp = (a8 + a9.*T + a10.*T.^2 + a11.*T.^3 + a12.*T.^4).*R;
            %Enthalpy
            H = (a8 + a9.*T./2 + a10.*T.^2./3 + a11.*T.^3./4 + a12.*T.^4./5+a13./T).*R.*T; 
            %Entropy
            S = (a8.*log(T) + a9.*T + a10.*T.^2./2 + a11.*T.^3./3 + a12.*T.^4./4 +a14).*R;
        end
end
end

Explaination-

1. Initially the file " THERMO.dat" is opend using the command fopen, then using fgetl comand diffrent lines values are obtained.

2.Then using fgetl command the file name is obtained from the first line.

3.Using strsplit command the string values are splitted,these splitted values are then converted to numbers using str2num to get global temperatures.

4.Now the species name and their local temperatures are obtained using strsplit and str2duoble.

5.Then the values of diffrent species are obtained form diffrent lines and assigned to diffrent variables such as a1,a2........a14.

4.Now to save the plots a new directory called mkdir is created and diffrent plots are saved into this.

5.Then a function program named coefficients is created in which specific heat , enthalpy and entropy formulas are coded and this function is made to call in the main program.

6.Now after obtaining the values of specific heat , enthalpy and entropy diffrent plots are made.

7.Then plots between temperature and specific heat,temperature and enthalpy,temperature and entropy are obtained using plot command, x andd y lables are named using xlabel and ylabel commands.

8.Now the obtained diffrent plots are stored in the form of .jpg format using save as command.

9.Then the molecular mass of diffrent species are caliculated now.

10.The caliculated molecular mass of diffrent species are printed onto the command window using fprintf command.

Outputs-

The following are the outputs obtained for diffrent species-

1.Plots of `O_2`

Temperature vs Specific heat Temperature vs Enthalpy

Temperature vs Entropy

2.Plots of `N_2`

Temperature vs Specific heat Temperature vs Enthalpy

Temperature vs Entropy

3.Plots of `CO_2`

Temperature vs Specific heat Temperature vs Enthalpy

Temperature vs Entropy

Molecular weights of diffrent species

The below fig shows diffrent species folders in which plots between Temperature vs specific heat , enthalpy and entropy are saved

Conclusion- Hence file parsing of nasa thermodynamics data is done along with plotting the plots between temperature vs entropy,enthalpy,specific heat & caliculating molecular weights of diffrent species .

References-

https://en.wikipedia.org/wiki/Parsing

https://www.youtube.com/watch?v=t0oNbYJn2Mw

https://www.mathworks.com/help/matlab/ref/mkdir.html

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.